1
0
mirror of https://github.com/home-assistant/supervisor.git synced 2025-12-24 12:29:08 +00:00
Files
supervisor/supervisor/dbus/payloads/generate.py
dependabot[bot] a85e816cd7 Bump pylint from 2.10.2 to 2.11.1 (#3128)
* Bump pylint from 2.10.2 to 2.11.1

Bumps [pylint](https://github.com/PyCQA/pylint) from 2.10.2 to 2.11.1.
- [Release notes](https://github.com/PyCQA/pylint/releases)
- [Changelog](https://github.com/PyCQA/pylint/blob/main/ChangeLog)
- [Commits](https://github.com/PyCQA/pylint/compare/v2.10.2...v2.11.1)

---
updated-dependencies:
- dependency-name: pylint
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Fix linter issues

* fix tests lint

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Pascal Vizeli <pvizeli@syshack.ch>
2021-09-22 14:33:32 +02:00

59 lines
1.7 KiB
Python

"""Payload generators for DBUS communication."""
from __future__ import annotations
from ipaddress import IPv4Address, IPv6Address
from pathlib import Path
import socket
from typing import TYPE_CHECKING
from uuid import uuid4
import jinja2
from ...host.const import InterfaceType
if TYPE_CHECKING:
from ...host.network import Interface
INTERFACE_UPDATE_TEMPLATE: Path = (
Path(__file__).parents[2].joinpath("dbus/payloads/interface_update.tmpl")
)
def interface_update_payload(
interface: Interface, name: str | None = None, uuid: str | None = None
) -> str:
"""Generate a payload for network interface update."""
env = jinja2.Environment()
def ipv4_to_int(ip_address: IPv4Address) -> int:
"""Convert an ipv4 to an int."""
return socket.htonl(int(ip_address))
def ipv6_to_byte(ip_address: IPv6Address) -> str:
"""Convert an ipv6 to an byte array."""
return f'[byte {", ".join(f"0x{val:02x}" for val in ip_address.packed)}]'
# Init template
env.filters["ipv4_to_int"] = ipv4_to_int
env.filters["ipv6_to_byte"] = ipv6_to_byte
template: jinja2.Template = env.from_string(INTERFACE_UPDATE_TEMPLATE.read_text())
# Generate UUID
if not uuid:
uuid = str(uuid4())
# Generate/Update ID/name
if not name or not name.startswith("Supervisor"):
name = f"Supervisor {interface.name}"
if interface.type == InterfaceType.VLAN:
name = f"{name}.{interface.vlan.id}"
# Fix SSID
if interface.wifi:
interface.wifi.ssid = ", ".join(
[f"0x{x}" for x in interface.wifi.ssid.encode().hex(",").split(",")]
)
return template.render(interface=interface, name=name, uuid=uuid)