Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Support for BT Home Hub 5."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import bthomehub5_devicelist
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
18 from homeassistant.helpers.typing import ConfigType
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 CONF_DEFAULT_IP = "192.168.1.254"
23 
24 PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
25  {vol.Optional(CONF_HOST, default=CONF_DEFAULT_IP): cv.string}
26 )
27 
28 
30  hass: HomeAssistant, config: ConfigType
31 ) -> BTHomeHub5DeviceScanner | None:
32  """Return a BT Home Hub 5 scanner if successful."""
33  scanner = BTHomeHub5DeviceScanner(config[DEVICE_TRACKER_DOMAIN])
34 
35  return scanner if scanner.success_init else None
36 
37 
38 class BTHomeHub5DeviceScanner(DeviceScanner):
39  """Class which queries a BT Home Hub 5."""
40 
41  def __init__(self, config):
42  """Initialise the scanner."""
43 
44  self.hosthost = config[CONF_HOST]
45  self.last_resultslast_results = {}
46 
47  # Test the router is accessible
48  data = bthomehub5_devicelist.get_devicelist(self.hosthost)
49  self.success_initsuccess_init = data is not None
50 
51  def scan_devices(self):
52  """Scan for new devices and return a list with found device IDs."""
53  self.update_infoupdate_info()
54 
55  return (device for device in self.last_resultslast_results)
56 
57  def get_device_name(self, device):
58  """Return the name of the given device or None if we don't know."""
59  # If not initialised and not already scanned and not found.
60  if device not in self.last_resultslast_results:
61  self.update_infoupdate_info()
62 
63  if not self.last_resultslast_results:
64  return None
65 
66  return self.last_resultslast_results.get(device)
67 
68  def update_info(self):
69  """Ensure the information from the BT Home Hub 5 is up to date."""
70 
71  _LOGGER.debug("Scanning")
72 
73  data = bthomehub5_devicelist.get_devicelist(self.hosthost)
74 
75  if not data:
76  _LOGGER.warning("Error scanning devices")
77  return
78 
79  self.last_resultslast_results = data
BTHomeHub5DeviceScanner|None get_scanner(HomeAssistant hass, ConfigType config)
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88