Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for LCN sensors."""
2 
3 from collections.abc import Iterable
4 from functools import partial
5 from itertools import chain
6 from typing import cast
7 
8 import pypck
9 
11  DOMAIN as DOMAIN_SENSOR,
12  SensorDeviceClass,
13  SensorEntity,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.const import (
17  CONF_DOMAIN,
18  CONF_ENTITIES,
19  CONF_SOURCE,
20  CONF_UNIT_OF_MEASUREMENT,
21 )
22 from homeassistant.core import HomeAssistant
23 from homeassistant.helpers.entity_platform import AddEntitiesCallback
24 from homeassistant.helpers.typing import ConfigType
25 
26 from .const import (
27  ADD_ENTITIES_CALLBACKS,
28  CONF_DOMAIN_DATA,
29  DOMAIN,
30  LED_PORTS,
31  S0_INPUTS,
32  SETPOINTS,
33  THRESHOLDS,
34  VARIABLES,
35 )
36 from .entity import LcnEntity
37 from .helpers import InputType
38 
39 DEVICE_CLASS_MAPPING = {
40  pypck.lcn_defs.VarUnit.CELSIUS: SensorDeviceClass.TEMPERATURE,
41  pypck.lcn_defs.VarUnit.KELVIN: SensorDeviceClass.TEMPERATURE,
42  pypck.lcn_defs.VarUnit.FAHRENHEIT: SensorDeviceClass.TEMPERATURE,
43  pypck.lcn_defs.VarUnit.LUX_T: SensorDeviceClass.ILLUMINANCE,
44  pypck.lcn_defs.VarUnit.LUX_I: SensorDeviceClass.ILLUMINANCE,
45  pypck.lcn_defs.VarUnit.METERPERSECOND: SensorDeviceClass.SPEED,
46  pypck.lcn_defs.VarUnit.VOLT: SensorDeviceClass.VOLTAGE,
47  pypck.lcn_defs.VarUnit.AMPERE: SensorDeviceClass.CURRENT,
48 }
49 
50 
52  config_entry: ConfigEntry,
53  async_add_entities: AddEntitiesCallback,
54  entity_configs: Iterable[ConfigType],
55 ) -> None:
56  """Add entities for this domain."""
57  entities: list[LcnVariableSensor | LcnLedLogicSensor] = []
58  for entity_config in entity_configs:
59  if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in chain(
60  VARIABLES, SETPOINTS, THRESHOLDS, S0_INPUTS
61  ):
62  entities.append(LcnVariableSensor(entity_config, config_entry))
63  else: # in LED_PORTS + LOGICOP_PORTS
64  entities.append(LcnLedLogicSensor(entity_config, config_entry))
65 
66  async_add_entities(entities)
67 
68 
70  hass: HomeAssistant,
71  config_entry: ConfigEntry,
72  async_add_entities: AddEntitiesCallback,
73 ) -> None:
74  """Set up LCN switch entities from a config entry."""
75  add_entities = partial(
76  add_lcn_entities,
77  config_entry,
78  async_add_entities,
79  )
80 
81  hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update(
82  {DOMAIN_SENSOR: add_entities}
83  )
84 
86  (
87  entity_config
88  for entity_config in config_entry.data[CONF_ENTITIES]
89  if entity_config[CONF_DOMAIN] == DOMAIN_SENSOR
90  ),
91  )
92 
93 
95  """Representation of a LCN sensor for variables."""
96 
97  def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
98  """Initialize the LCN sensor."""
99  super().__init__(config, config_entry)
100 
101  self.variablevariable = pypck.lcn_defs.Var[config[CONF_DOMAIN_DATA][CONF_SOURCE]]
102  self.unitunit = pypck.lcn_defs.VarUnit.parse(
103  config[CONF_DOMAIN_DATA][CONF_UNIT_OF_MEASUREMENT]
104  )
105 
106  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = cast(str, self.unitunit.value)
107  self._attr_device_class_attr_device_class = DEVICE_CLASS_MAPPING.get(self.unitunit, None)
108 
109  async def async_added_to_hass(self) -> None:
110  """Run when entity about to be added to hass."""
111  await super().async_added_to_hass()
112  if not self.device_connectiondevice_connection.is_group:
113  await self.device_connectiondevice_connection.activate_status_request_handler(self.variablevariable)
114 
115  async def async_will_remove_from_hass(self) -> None:
116  """Run when entity will be removed from hass."""
117  await super().async_will_remove_from_hass()
118  if not self.device_connectiondevice_connection.is_group:
119  await self.device_connectiondevice_connection.cancel_status_request_handler(self.variablevariable)
120 
121  def input_received(self, input_obj: InputType) -> None:
122  """Set sensor value when LCN input object (command) is received."""
123  if (
124  not isinstance(input_obj, pypck.inputs.ModStatusVar)
125  or input_obj.get_var() != self.variablevariable
126  ):
127  return
128 
129  is_regulator = self.variablevariable.name in SETPOINTS
130  self._attr_native_value_attr_native_value = input_obj.get_value().to_var_unit(
131  self.unitunit, is_regulator
132  )
133 
134  self.async_write_ha_stateasync_write_ha_state()
135 
136 
138  """Representation of a LCN sensor for leds and logicops."""
139 
140  def __init__(self, config: ConfigType, config_entry: ConfigEntry) -> None:
141  """Initialize the LCN sensor."""
142  super().__init__(config, config_entry)
143 
144  if config[CONF_DOMAIN_DATA][CONF_SOURCE] in LED_PORTS:
145  self.sourcesource = pypck.lcn_defs.LedPort[config[CONF_DOMAIN_DATA][CONF_SOURCE]]
146  else:
147  self.sourcesource = pypck.lcn_defs.LogicOpPort[
148  config[CONF_DOMAIN_DATA][CONF_SOURCE]
149  ]
150 
151  async def async_added_to_hass(self) -> None:
152  """Run when entity about to be added to hass."""
153  await super().async_added_to_hass()
154  if not self.device_connectiondevice_connection.is_group:
155  await self.device_connectiondevice_connection.activate_status_request_handler(self.sourcesource)
156 
157  async def async_will_remove_from_hass(self) -> None:
158  """Run when entity will be removed from hass."""
159  await super().async_will_remove_from_hass()
160  if not self.device_connectiondevice_connection.is_group:
161  await self.device_connectiondevice_connection.cancel_status_request_handler(self.sourcesource)
162 
163  def input_received(self, input_obj: InputType) -> None:
164  """Set sensor value when LCN input object (command) is received."""
165  if not isinstance(input_obj, pypck.inputs.ModStatusLedsAndLogicOps):
166  return
167 
168  if self.sourcesource in pypck.lcn_defs.LedPort:
169  self._attr_native_value_attr_native_value = input_obj.get_led_state(
170  self.sourcesource.value
171  ).name.lower()
172  elif self.sourcesource in pypck.lcn_defs.LogicOpPort:
173  self._attr_native_value_attr_native_value = input_obj.get_logic_op_state(
174  self.sourcesource.value
175  ).name.lower()
176 
177  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, ConfigType config, ConfigEntry config_entry)
Definition: sensor.py:140
None input_received(self, InputType input_obj)
Definition: sensor.py:163
None __init__(self, ConfigType config, ConfigEntry config_entry)
Definition: sensor.py:97
None input_received(self, InputType input_obj)
Definition: sensor.py:121
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
None add_lcn_entities(ConfigEntry config_entry, AddEntitiesCallback async_add_entities, Iterable[ConfigType] entity_configs)
Definition: sensor.py:55
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:73