Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for VersaSense actuator peripheral."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from homeassistant.components.switch import SwitchEntity
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
12 
13 from . import DOMAIN
14 from .const import (
15  KEY_CONSUMER,
16  KEY_IDENTIFIER,
17  KEY_MEASUREMENT,
18  KEY_PARENT_MAC,
19  KEY_PARENT_NAME,
20  KEY_UNIT,
21  PERIPHERAL_STATE_OFF,
22  PERIPHERAL_STATE_ON,
23 )
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
29  hass: HomeAssistant,
30  config: ConfigType,
31  async_add_entities: AddEntitiesCallback,
32  discovery_info: DiscoveryInfoType | None = None,
33 ) -> None:
34  """Set up actuator platform."""
35  if discovery_info is None:
36  return
37 
38  consumer = hass.data[DOMAIN][KEY_CONSUMER]
39 
40  actuator_list = []
41 
42  for entity_info in discovery_info.values():
43  peripheral = hass.data[DOMAIN][entity_info[KEY_PARENT_MAC]][
44  entity_info[KEY_IDENTIFIER]
45  ]
46  parent_name = entity_info[KEY_PARENT_NAME]
47  unit = entity_info[KEY_UNIT]
48  measurement = entity_info[KEY_MEASUREMENT]
49 
50  actuator_list.append(
51  VActuator(peripheral, parent_name, unit, measurement, consumer)
52  )
53 
54  async_add_entities(actuator_list)
55 
56 
58  """Representation of an Actuator."""
59 
60  def __init__(self, peripheral, parent_name, unit, measurement, consumer):
61  """Initialize the sensor."""
62  self._is_on_is_on = False
63  self._available_available = True
64  self._name_name = f"{parent_name} {measurement}"
65  self._parent_mac_parent_mac = peripheral.parentMac
66  self._identifier_identifier = peripheral.identifier
67  self._unit_unit = unit
68  self._measurement_measurement = measurement
69  self.consumerconsumer = consumer
70 
71  @property
72  def unique_id(self):
73  """Return the unique id of the actuator."""
74  return f"{self._parent_mac}/{self._identifier}/{self._measurement}"
75 
76  @property
77  def name(self):
78  """Return the name of the actuator."""
79  return self._name_name
80 
81  @property
82  def is_on(self):
83  """Return the state of the actuator."""
84  return self._is_on_is_on
85 
86  @property
87  def available(self):
88  """Return if the actuator is available."""
89  return self._available_available
90 
91  async def async_turn_off(self, **kwargs: Any) -> None:
92  """Turn off the actuator."""
93  await self.update_stateupdate_state(0)
94 
95  async def async_turn_on(self, **kwargs: Any) -> None:
96  """Turn on the actuator."""
97  await self.update_stateupdate_state(1)
98 
99  async def update_state(self, state):
100  """Update the state of the actuator."""
101  payload = {"id": "state-num", "value": state}
102 
103  await self.consumerconsumer.actuatePeripheral(
104  None, self._identifier_identifier, self._parent_mac_parent_mac, payload
105  )
106 
107  async def async_update(self) -> None:
108  """Fetch state data from the actuator."""
109  samples = await self.consumerconsumer.fetchPeripheralSample(
110  None, self._identifier_identifier, self._parent_mac_parent_mac
111  )
112 
113  if samples is not None:
114  for sample in samples:
115  if sample.measurement == self._measurement_measurement:
116  self._available_available = True
117  if sample.value == PERIPHERAL_STATE_OFF:
118  self._is_on_is_on = False
119  elif sample.value == PERIPHERAL_STATE_ON:
120  self._is_on_is_on = True
121  break
122  else:
123  _LOGGER.error("Sample unavailable")
124  self._available_available = False
125  self._is_on_is_on = None
def __init__(self, peripheral, parent_name, unit, measurement, consumer)
Definition: switch.py:60
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:33