Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for VersaSense sensor peripheral."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from homeassistant.components.sensor import SensorEntity
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
11 
12 from . import DOMAIN
13 from .const import (
14  KEY_CONSUMER,
15  KEY_IDENTIFIER,
16  KEY_MEASUREMENT,
17  KEY_PARENT_MAC,
18  KEY_PARENT_NAME,
19  KEY_UNIT,
20 )
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 
26  hass: HomeAssistant,
27  config: ConfigType,
28  async_add_entities: AddEntitiesCallback,
29  discovery_info: DiscoveryInfoType | None = None,
30 ) -> None:
31  """Set up the sensor platform."""
32  if discovery_info is None:
33  return
34 
35  consumer = hass.data[DOMAIN][KEY_CONSUMER]
36 
37  sensor_list = []
38 
39  for entity_info in discovery_info.values():
40  peripheral = hass.data[DOMAIN][entity_info[KEY_PARENT_MAC]][
41  entity_info[KEY_IDENTIFIER]
42  ]
43  parent_name = entity_info[KEY_PARENT_NAME]
44  unit = entity_info[KEY_UNIT]
45  measurement = entity_info[KEY_MEASUREMENT]
46 
47  sensor_list.append(
48  VSensor(peripheral, parent_name, unit, measurement, consumer)
49  )
50 
51  async_add_entities(sensor_list)
52 
53 
55  """Representation of a Sensor."""
56 
57  def __init__(self, peripheral, parent_name, unit, measurement, consumer):
58  """Initialize the sensor."""
59  self._state_state = None
60  self._available_available = True
61  self._name_name = f"{parent_name} {measurement}"
62  self._parent_mac_parent_mac = peripheral.parentMac
63  self._identifier_identifier = peripheral.identifier
64  self._unit_unit = unit
65  self._measurement_measurement = measurement
66  self.consumerconsumer = consumer
67 
68  @property
69  def unique_id(self):
70  """Return the unique id of the sensor."""
71  return f"{self._parent_mac}/{self._identifier}/{self._measurement}"
72 
73  @property
74  def name(self):
75  """Return the name of the sensor."""
76  return self._name_name
77 
78  @property
79  def native_value(self):
80  """Return the state of the sensor."""
81  return self._state_state
82 
83  @property
85  """Return the unit of measurement."""
86  return self._unit_unit
87 
88  @property
89  def available(self):
90  """Return if the sensor is available."""
91  return self._available_available
92 
93  async def async_update(self) -> None:
94  """Fetch new state data for the sensor."""
95  samples = await self.consumerconsumer.fetchPeripheralSample(
96  None, self._identifier_identifier, self._parent_mac_parent_mac
97  )
98 
99  if samples is not None:
100  for sample in samples:
101  if sample.measurement == self._measurement_measurement:
102  self._available_available = True
103  self._state_state = sample.value
104  break
105  else:
106  _LOGGER.error("Sample unavailable")
107  self._available_available = False
108  self._state_state = None
def __init__(self, peripheral, parent_name, unit, measurement, consumer)
Definition: sensor.py:57
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:30