1 """Support for VersaSense sensor peripheral."""
3 from __future__
import annotations
22 _LOGGER = logging.getLogger(__name__)
28 async_add_entities: AddEntitiesCallback,
29 discovery_info: DiscoveryInfoType |
None =
None,
31 """Set up the sensor platform."""
32 if discovery_info
is None:
35 consumer = hass.data[DOMAIN][KEY_CONSUMER]
39 for entity_info
in discovery_info.values():
40 peripheral = hass.data[DOMAIN][entity_info[KEY_PARENT_MAC]][
41 entity_info[KEY_IDENTIFIER]
43 parent_name = entity_info[KEY_PARENT_NAME]
44 unit = entity_info[KEY_UNIT]
45 measurement = entity_info[KEY_MEASUREMENT]
48 VSensor(peripheral, parent_name, unit, measurement, consumer)
55 """Representation of a Sensor."""
57 def __init__(self, peripheral, parent_name, unit, measurement, consumer):
58 """Initialize the sensor."""
61 self.
_name_name = f
"{parent_name} {measurement}"
70 """Return the unique id of the sensor."""
71 return f
"{self._parent_mac}/{self._identifier}/{self._measurement}"
75 """Return the name of the sensor."""
76 return self.
_name_name
80 """Return the state of the sensor."""
85 """Return the unit of measurement."""
86 return self.
_unit_unit
90 """Return if the sensor is available."""
94 """Fetch new state data for the sensor."""
95 samples = await self.
consumerconsumer.fetchPeripheralSample(
99 if samples
is not None:
100 for sample
in samples:
103 self.
_state_state = sample.value
106 _LOGGER.error(
"Sample unavailable")
def __init__(self, peripheral, parent_name, unit, measurement, consumer)
def native_unit_of_measurement(self)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)