1 """Support for Swisscom routers (Internet-Box)."""
3 from __future__
import annotations
5 from contextlib
import suppress
9 import voluptuous
as vol
12 DOMAIN
as DEVICE_TRACKER_DOMAIN,
13 PLATFORM_SCHEMA
as DEVICE_TRACKER_PLATFORM_SCHEMA,
21 _LOGGER = logging.getLogger(__name__)
23 DEFAULT_IP =
"192.168.1.1"
25 PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
26 {vol.Optional(CONF_HOST, default=DEFAULT_IP): cv.string}
31 hass: HomeAssistant, config: ConfigType
32 ) -> SwisscomDeviceScanner |
None:
33 """Return the Swisscom device scanner."""
36 return scanner
if scanner.success_init
else None
40 """Class which queries a router running Swisscom Internet-Box firmware."""
43 """Initialize the scanner."""
44 self.
hosthost = config[CONF_HOST]
52 """Scan for new devices and return a list with found device IDs."""
54 return [client[
"mac"]
for client
in self.
last_resultslast_results]
57 """Return the name of the given device or None if we don't know."""
61 if client[
"mac"] == device:
66 """Ensure the information from the Swisscom router is up to date.
68 Return boolean if scanning successful.
73 _LOGGER.debug(
"Loading data from Swisscom Internet Box")
77 active_clients = [client
for client
in data.values()
if client[
"status"]]
82 """Retrieve data from Swisscom and return parsed result."""
83 url = f
"http://{self.host}/ws"
84 headers = {
"Content-Type":
"application/x-sah-ws-4-call+json"}
86 {"service":"Devices", "method":"get",
87 "parameters":{"expression":"lan and not self"}}"""
92 request = requests.post(url, headers=headers, data=data, timeout=10)
94 requests.exceptions.ConnectionError,
95 requests.exceptions.Timeout,
96 requests.exceptions.ConnectTimeout,
98 _LOGGER.debug(
"No response from Swisscom Internet Box")
101 if "status" not in request.json():
102 _LOGGER.debug(
"No status in response from Swisscom Internet Box")
105 for device
in request.json()[
"status"]:
106 with suppress(KeyError, requests.exceptions.RequestException):
107 devices[device[
"Key"]] = {
108 "ip": device[
"IPAddress"],
109 "mac": device[
"PhysAddress"],
110 "host": device[
"Name"],
111 "status": device[
"Active"],
def __init__(self, config)
def get_device_name(self, device)
def get_swisscom_data(self)
SwisscomDeviceScanner|None get_scanner(HomeAssistant hass, ConfigType config)