Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Support for Arris TG2492LG router."""
2 
3 from __future__ import annotations
4 
5 from aiohttp.client_exceptions import ClientResponseError
6 from arris_tg2492lg import ConnectBox, Device
7 import voluptuous as vol
8 
10  DOMAIN as DEVICE_TRACKER_DOMAIN,
11  PLATFORM_SCHEMA as DEVICE_TRACKER_PLATFORM_SCHEMA,
12  DeviceScanner,
13 )
14 from homeassistant.const import CONF_HOST, CONF_PASSWORD
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.aiohttp_client import async_get_clientsession
18 from homeassistant.helpers.typing import ConfigType
19 
20 DEFAULT_HOST = "192.168.178.1"
21 
22 PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
23  {
24  vol.Required(CONF_PASSWORD): cv.string,
25  vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
26  }
27 )
28 
29 
31  hass: HomeAssistant, config: ConfigType
32 ) -> ArrisDeviceScanner | None:
33  """Return the Arris device scanner if successful."""
34  conf = config[DEVICE_TRACKER_DOMAIN]
35  url = f"http://{conf[CONF_HOST]}"
36  websession = async_get_clientsession(hass)
37  connect_box = ConnectBox(websession, url, conf[CONF_PASSWORD])
38 
39  try:
40  await connect_box.async_login()
41 
42  return ArrisDeviceScanner(connect_box)
43  except ClientResponseError:
44  return None
45 
46 
47 class ArrisDeviceScanner(DeviceScanner):
48  """Class which queries a Arris TG2492LG router for connected devices."""
49 
50  def __init__(self, connect_box: ConnectBox) -> None:
51  """Initialize the scanner."""
52  self.connect_boxconnect_box = connect_box
53  self.last_resultslast_results: list[Device] = []
54 
55  async def async_scan_devices(self) -> list[str]:
56  """Scan for new devices and return a list with found device IDs."""
57  await self._async_update_info_async_update_info()
58 
59  return [device.mac for device in self.last_resultslast_results if device.mac]
60 
61  async def async_get_device_name(self, device: str) -> str | None:
62  """Return the name of the given device or None if we don't know."""
63  return next(
64  (result.hostname for result in self.last_resultslast_results if result.mac == device),
65  None,
66  )
67 
68  async def _async_update_info(self) -> None:
69  """Ensure the information from the Arris TG2492LG router is up to date."""
70  result = await self.connect_boxconnect_box.async_get_connected_devices()
71 
72  last_results: list[Device] = []
73  mac_addresses: set[str | None] = set()
74 
75  for device in result:
76  if device.online and device.mac not in mac_addresses:
77  last_results.append(device)
78  mac_addresses.add(device.mac)
79 
80  self.last_resultslast_results = last_results
ArrisDeviceScanner|None async_get_scanner(HomeAssistant hass, ConfigType config)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)