Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Support for Cisco Mobility Express."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from ciscomobilityexpress.ciscome import CiscoMobilityExpress
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 (
16  CONF_HOST,
17  CONF_PASSWORD,
18  CONF_SSL,
19  CONF_USERNAME,
20  CONF_VERIFY_SSL,
21 )
22 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.typing import ConfigType
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 DEFAULT_SSL = False
29 DEFAULT_VERIFY_SSL = True
30 
31 PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
32  {
33  vol.Required(CONF_HOST): cv.string,
34  vol.Required(CONF_USERNAME): cv.string,
35  vol.Required(CONF_PASSWORD): cv.string,
36  vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
37  vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
38  }
39 )
40 
41 
42 def get_scanner(hass: HomeAssistant, config: ConfigType) -> CiscoMEDeviceScanner | None:
43  """Validate the configuration and return a Cisco ME scanner."""
44 
45  config = config[DEVICE_TRACKER_DOMAIN]
46 
47  controller = CiscoMobilityExpress(
48  config[CONF_HOST],
49  config[CONF_USERNAME],
50  config[CONF_PASSWORD],
51  config[CONF_SSL],
52  config[CONF_VERIFY_SSL],
53  )
54  if not controller.is_logged_in():
55  return None
56  return CiscoMEDeviceScanner(controller)
57 
58 
59 class CiscoMEDeviceScanner(DeviceScanner):
60  """Scanner for devices associated to a Cisco ME controller."""
61 
62  def __init__(self, controller):
63  """Initialize the scanner."""
64  self.controllercontroller = controller
65  self.last_resultslast_results = {}
66 
67  def scan_devices(self):
68  """Scan for new devices and return a list with found device IDs."""
69  self._update_info_update_info()
70 
71  return [device.macaddr for device in self.last_resultslast_results]
72 
73  def get_device_name(self, device):
74  """Return the name of the given device or None if we don't know."""
75  return next(
76  (result.clId for result in self.last_resultslast_results if result.macaddr == device),
77  None,
78  )
79 
80  def get_extra_attributes(self, device):
81  """Get extra attributes of a device.
82 
83  Some known extra attributes that may be returned in the device tuple
84  include SSID, PT (eg 802.11ac), devtype (eg iPhone 7) among others.
85  """
86  device = next(
87  (result for result in self.last_resultslast_results if result.macaddr == device), None
88  )
89  return device._asdict()
90 
91  def _update_info(self):
92  """Check the Cisco ME controller for devices."""
93  self.last_resultslast_results = self.controllercontroller.get_associated_devices()
94  _LOGGER.debug(
95  "Cisco Mobility Express controller returned: %s", self.last_resultslast_results
96  )
CiscoMEDeviceScanner|None get_scanner(HomeAssistant hass, ConfigType config)