Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Support for Sky Hub."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from pyskyqhub.skyq_hub import SkyQHub
8 import voluptuous as vol
9 
11  DOMAIN as DEVICE_TRACKER_DOMAIN,
12  PLATFORM_SCHEMA as DEVICE_TRACKER_PLATFORM_SCHEMA,
13  DeviceScanner,
14 )
15 from homeassistant.const import CONF_HOST
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.aiohttp_client import async_get_clientsession
19 from homeassistant.helpers.typing import ConfigType
20 
21 _LOGGER = logging.getLogger(__name__)
22 
23 PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
24  {vol.Optional(CONF_HOST): cv.string}
25 )
26 
27 
29  hass: HomeAssistant, config: ConfigType
30 ) -> SkyHubDeviceScanner | None:
31  """Return a Sky Hub scanner if successful."""
32  host = config[DEVICE_TRACKER_DOMAIN].get(CONF_HOST, "192.168.1.254")
33  websession = async_get_clientsession(hass)
34  hub = SkyQHub(websession, host)
35 
36  _LOGGER.debug("Initialising Sky Hub")
37  await hub.async_connect()
38  if hub.success_init:
39  return SkyHubDeviceScanner(hub)
40 
41  return None
42 
43 
44 class SkyHubDeviceScanner(DeviceScanner):
45  """Class which queries a Sky Hub router."""
46 
47  def __init__(self, hub):
48  """Initialise the scanner."""
49  self._hub_hub = hub
50  self.last_resultslast_results = {}
51 
52  async def async_scan_devices(self):
53  """Scan for new devices and return a list with found device IDs."""
54  await self._async_update_info_async_update_info()
55  return [device.mac for device in self.last_resultslast_results]
56 
57  async def async_get_device_name(self, device):
58  """Return the name of the given device."""
59  return next(
60  (result.name for result in self.last_resultslast_results if result.mac == device),
61  None,
62  )
63 
64  async def async_get_extra_attributes(self, device):
65  """Get extra attributes of a device."""
66  device = next(
67  (result for result in self.last_resultslast_results if result.mac == device), None
68  )
69  if device is None:
70  return {}
71 
72  return device.asdict()
73 
74  async def _async_update_info(self):
75  """Ensure the information from the Sky Hub is up to date."""
76  _LOGGER.debug("Scanning")
77 
78  if not (data := await self._hub_hub.async_get_skyhub_data()):
79  return
80 
81  self.last_resultslast_results = data
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
SkyHubDeviceScanner|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)