Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Support for UPC ConnectBox router."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from connect_box import ConnectBox
8 from connect_box.exceptions import ConnectBoxError, ConnectBoxLoginError
9 import voluptuous as vol
10 
12  DOMAIN as DEVICE_TRACKER_DOMAIN,
13  PLATFORM_SCHEMA as DEVICE_TRACKER_PLATFORM_SCHEMA,
14  DeviceScanner,
15 )
16 from homeassistant.const import CONF_HOST, CONF_PASSWORD, EVENT_HOMEASSISTANT_STOP
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.aiohttp_client import async_get_clientsession
20 from homeassistant.helpers.typing import ConfigType
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 DEFAULT_IP = "192.168.0.1"
25 
26 PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
27  {
28  vol.Required(CONF_PASSWORD): cv.string,
29  vol.Optional(CONF_HOST, default=DEFAULT_IP): cv.string,
30  }
31 )
32 
33 
35  hass: HomeAssistant, config: ConfigType
36 ) -> UPCDeviceScanner | None:
37  """Return the UPC device scanner."""
38  conf = config[DEVICE_TRACKER_DOMAIN]
39  session = async_get_clientsession(hass)
40  connect_box = ConnectBox(session, conf[CONF_PASSWORD], host=conf[CONF_HOST])
41 
42  # Check login data
43  try:
44  await connect_box.async_initialize_token()
45  except ConnectBoxLoginError:
46  _LOGGER.error("ConnectBox login data error!")
47  return None
48  except ConnectBoxError:
49  pass
50 
51  async def _shutdown(event):
52  """Shutdown event."""
53  await connect_box.async_close_session()
54 
55  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown)
56 
57  return UPCDeviceScanner(connect_box)
58 
59 
60 class UPCDeviceScanner(DeviceScanner):
61  """Class which queries a router running UPC ConnectBox firmware."""
62 
63  def __init__(self, connect_box: ConnectBox) -> None:
64  """Initialize the scanner."""
65  self.connect_box: ConnectBox = connect_box
66 
67  async def async_scan_devices(self) -> list[str]:
68  """Scan for new devices and return a list with found device IDs."""
69  try:
70  await self.connect_box.async_get_devices()
71  except ConnectBoxError:
72  return []
73 
74  return [device.mac for device in self.connect_box.devices]
75 
76  async def async_get_device_name(self, device: str) -> str | None:
77  """Get the device name (the name of the wireless device not used)."""
78  for connected_device in self.connect_box.devices:
79  if (
80  connected_device.mac == device
81  and connected_device.hostname.lower() != "unknown"
82  ):
83  return connected_device.hostname
84 
85  return None
Generator[ProtectAdoptableDeviceModel] async_get_devices(Bootstrap bootstrap, Iterable[ModelType] model_type)
Definition: utils.py:85
UPCDeviceScanner|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)