Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Support for Xiaomi Mi WiFi Repeater 2."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from miio import DeviceException, WifiRepeater
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, CONF_TOKEN
16 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.typing import ConfigType
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
23  {
24  vol.Required(CONF_HOST): cv.string,
25  vol.Required(CONF_TOKEN): vol.All(cv.string, vol.Length(min=32, max=32)),
26  }
27 )
28 
29 
31  hass: HomeAssistant, config: ConfigType
32 ) -> XiaomiMiioDeviceScanner | None:
33  """Return a Xiaomi MiIO device scanner."""
34  scanner = None
35  config = config[DEVICE_TRACKER_DOMAIN]
36 
37  host = config[CONF_HOST]
38  token = config[CONF_TOKEN]
39 
40  _LOGGER.debug("Initializing with host %s (token %s...)", host, token[:5])
41 
42  try:
43  device = WifiRepeater(host, token)
44  device_info = device.info()
45  _LOGGER.debug(
46  "%s %s %s detected",
47  device_info.model,
48  device_info.firmware_version,
49  device_info.hardware_version,
50  )
51  scanner = XiaomiMiioDeviceScanner(device)
52  except DeviceException as ex:
53  _LOGGER.error("Device unavailable or token incorrect: %s", ex)
54 
55  return scanner
56 
57 
58 class XiaomiMiioDeviceScanner(DeviceScanner):
59  """Class which queries a Xiaomi Mi WiFi Repeater."""
60 
61  def __init__(self, device):
62  """Initialize the scanner."""
63  self.devicedevice = device
64 
65  async def async_scan_devices(self):
66  """Scan for devices and return a list containing found device IDs."""
67  try:
68  station_info = await self.hass.async_add_executor_job(self.devicedevice.status)
69  _LOGGER.debug("Got new station info: %s", station_info)
70  except DeviceException as ex:
71  _LOGGER.error("Unable to fetch the state: %s", ex)
72  return []
73 
74  return [device["mac"] for device in station_info.associated_stations]
75 
76  async def async_get_device_name(self, device: str) -> str | None:
77  """Return None.
78 
79  The repeater doesn't provide the name of the associated device.
80  """
81  return None
XiaomiMiioDeviceScanner|None get_scanner(HomeAssistant hass, ConfigType config)