Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for VersaSense MicroPnP devices."""
2 
3 import logging
4 
5 import pyversasense as pyv
6 import voluptuous as vol
7 
8 from homeassistant.const import CONF_HOST, Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers import aiohttp_client
12 from homeassistant.helpers.discovery import async_load_platform
13 from homeassistant.helpers.typing import ConfigType
14 
15 from .const import (
16  KEY_CONSUMER,
17  KEY_IDENTIFIER,
18  KEY_MEASUREMENT,
19  KEY_PARENT_MAC,
20  KEY_PARENT_NAME,
21  KEY_UNIT,
22  PERIPHERAL_CLASS_SENSOR,
23  PERIPHERAL_CLASS_SENSOR_ACTUATOR,
24 )
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 DOMAIN = "versasense"
29 
30 # Validation of the user's configuration
31 CONFIG_SCHEMA = vol.Schema(
32  {DOMAIN: vol.Schema({vol.Required(CONF_HOST): cv.string})}, extra=vol.ALLOW_EXTRA
33 )
34 
35 
36 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
37  """Set up the versasense component."""
38  session = aiohttp_client.async_get_clientsession(hass)
39  consumer = pyv.Consumer(config[DOMAIN]["host"], session)
40 
41  hass.data[DOMAIN] = {KEY_CONSUMER: consumer}
42 
43  await _configure_entities(hass, config, consumer)
44 
45  # Return boolean to indicate that initialization was successful.
46  return True
47 
48 
49 async def _configure_entities(hass, config, consumer):
50  """Fetch all devices with their peripherals for representation."""
51  devices = await consumer.fetchDevices()
52  _LOGGER.debug(devices)
53 
54  sensor_info = {}
55  switch_info = {}
56 
57  for mac, device in devices.items():
58  _LOGGER.debug("Device connected: %s %s", device.name, mac)
59  hass.data[DOMAIN][mac] = {}
60 
61  for peripheral_id, peripheral in device.peripherals.items():
62  hass.data[DOMAIN][mac][peripheral_id] = peripheral
63 
64  if peripheral.classification == PERIPHERAL_CLASS_SENSOR:
65  sensor_info = _add_entity_info(peripheral, device, sensor_info)
66  elif peripheral.classification == PERIPHERAL_CLASS_SENSOR_ACTUATOR:
67  switch_info = _add_entity_info(peripheral, device, switch_info)
68 
69  if sensor_info:
70  _load_platform(hass, config, Platform.SENSOR, sensor_info)
71 
72  if switch_info:
73  _load_platform(hass, config, Platform.SWITCH, switch_info)
74 
75 
76 def _add_entity_info(peripheral, device, entity_dict) -> None:
77  """Add info from a peripheral to specified list."""
78  for measurement in peripheral.measurements:
79  entity_info = {
80  KEY_IDENTIFIER: peripheral.identifier,
81  KEY_UNIT: measurement.unit,
82  KEY_MEASUREMENT: measurement.name,
83  KEY_PARENT_NAME: device.name,
84  KEY_PARENT_MAC: device.mac,
85  }
86 
87  key = f"{entity_info[KEY_PARENT_MAC]}/{entity_info[KEY_IDENTIFIER]}/{entity_info[KEY_MEASUREMENT]}"
88  entity_dict[key] = entity_info
89 
90  return entity_dict
91 
92 
93 def _load_platform(hass, config, entity_type, entity_info):
94  """Load platform with list of entity info."""
95  hass.async_create_task(
96  async_load_platform(hass, entity_type, DOMAIN, entity_info, config)
97  )
None _add_entity_info(peripheral, device, entity_dict)
Definition: __init__.py:76
def _configure_entities(hass, config, consumer)
Definition: __init__.py:49
def _load_platform(hass, config, entity_type, entity_info)
Definition: __init__.py:93
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:36
None async_load_platform(core.HomeAssistant hass, Platform|str component, str platform, DiscoveryInfoType|None discovered, ConfigType hass_config)
Definition: discovery.py:152