Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor platform integration for ADC ports of Numato USB GPIO expanders."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from numato_gpio import NumatoGpioError
8 
9 from homeassistant.components.sensor import SensorEntity
10 from homeassistant.const import CONF_ID, CONF_NAME, CONF_SENSORS
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
14 
15 from . import (
16  CONF_DEVICES,
17  CONF_DST_RANGE,
18  CONF_DST_UNIT,
19  CONF_PORTS,
20  CONF_SRC_RANGE,
21  DATA_API,
22  DOMAIN,
23 )
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
29  hass: HomeAssistant,
30  config: ConfigType,
31  add_entities: AddEntitiesCallback,
32  discovery_info: DiscoveryInfoType | None = None,
33 ) -> None:
34  """Set up the configured Numato USB GPIO ADC sensor ports."""
35  if discovery_info is None:
36  return
37 
38  api = hass.data[DOMAIN][DATA_API]
39  sensors = []
40  devices = hass.data[DOMAIN][CONF_DEVICES]
41  for device in [d for d in devices if CONF_SENSORS in d]:
42  device_id = device[CONF_ID]
43  ports = device[CONF_SENSORS][CONF_PORTS]
44  for port, adc_def in ports.items():
45  try:
46  api.setup_input(device_id, port)
47  except NumatoGpioError as err:
48  _LOGGER.error(
49  "Failed to initialize sensor '%s' on Numato device %s port %s: %s",
50  adc_def[CONF_NAME],
51  device_id,
52  port,
53  err,
54  )
55  continue
56  sensors.append(
58  adc_def[CONF_NAME],
59  device_id,
60  port,
61  adc_def[CONF_SRC_RANGE],
62  adc_def[CONF_DST_RANGE],
63  adc_def[CONF_DST_UNIT],
64  api,
65  )
66  )
67  add_entities(sensors, True)
68 
69 
71  """Represents an ADC port of a Numato USB GPIO expander."""
72 
73  _attr_icon = "mdi:gauge"
74 
75  def __init__(self, name, device_id, port, src_range, dst_range, dst_unit, api):
76  """Initialize the sensor."""
77  self._attr_name_attr_name = name
78  self._device_id_device_id = device_id
79  self._port_port = port
80  self._src_range_src_range = src_range
81  self._dst_range_dst_range = dst_range
82  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = dst_unit
83  self._api_api = api
84 
85  def update(self) -> None:
86  """Get the latest data and updates the state."""
87  try:
88  adc_val = self._api_api.read_adc_input(self._device_id_device_id, self._port_port)
89  adc_val = self._clamp_to_source_range_clamp_to_source_range(adc_val)
90  self._attr_native_value_attr_native_value = self._linear_scale_to_dest_range_linear_scale_to_dest_range(adc_val)
91  except NumatoGpioError as err:
92  self._attr_native_value_attr_native_value = None
93  _LOGGER.error(
94  "Failed to update Numato device %s ADC-port %s: %s",
95  self._device_id_device_id,
96  self._port_port,
97  err,
98  )
99 
100  def _clamp_to_source_range(self, val):
101  # clamp to source range
102  val = max(val, self._src_range_src_range[0])
103  return min(val, self._src_range_src_range[1])
104 
106  # linear scale to dest range
107  src_len = self._src_range_src_range[1] - self._src_range_src_range[0]
108  adc_val_rel = val - self._src_range_src_range[0]
109  ratio = float(adc_val_rel) / float(src_len)
110  dst_len = self._dst_range_dst_range[1] - self._dst_range_dst_range[0]
111  return self._dst_range_dst_range[0] + ratio * dst_len
def __init__(self, name, device_id, port, src_range, dst_range, dst_unit, api)
Definition: sensor.py:75
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:33