Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Plaato Airlock sensors."""
2 
3 from __future__ import annotations
4 
5 from pyplaato.models.device import PlaatoDevice
6 from pyplaato.plaato import PlaatoKeg
7 
8 from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant, callback
12  async_dispatcher_connect,
13  async_dispatcher_send,
14 )
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
17 
18 from . import ATTR_TEMP, SENSOR_UPDATE
19 from .const import (
20  CONF_USE_WEBHOOK,
21  COORDINATOR,
22  DEVICE,
23  DEVICE_ID,
24  DOMAIN,
25  SENSOR_DATA,
26  SENSOR_SIGNAL,
27 )
28 from .entity import PlaatoEntity
29 
30 
32  hass: HomeAssistant,
33  config: ConfigType,
34  async_add_entities: AddEntitiesCallback,
35  discovery_info: DiscoveryInfoType | None = None,
36 ) -> None:
37  """Set up the Plaato sensor."""
38 
39 
41  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
42 ) -> None:
43  """Set up Plaato from a config entry."""
44  entry_data = hass.data[DOMAIN][entry.entry_id]
45 
46  @callback
47  def _async_update_from_webhook(device_id, sensor_data: PlaatoDevice):
48  """Update/Create the sensors."""
49  entry_data[SENSOR_DATA] = sensor_data
50 
51  if device_id != entry_data[DEVICE][DEVICE_ID]:
52  entry_data[DEVICE][DEVICE_ID] = device_id
54  [
55  PlaatoSensor(entry_data, sensor_type)
56  for sensor_type in sensor_data.sensors
57  ]
58  )
59  else:
60  for sensor_type in sensor_data.sensors:
61  async_dispatcher_send(hass, SENSOR_SIGNAL % (device_id, sensor_type))
62 
63  if entry.data[CONF_USE_WEBHOOK]:
64  async_dispatcher_connect(hass, SENSOR_UPDATE, _async_update_from_webhook)
65  else:
66  coordinator = entry_data[COORDINATOR]
68  PlaatoSensor(entry_data, sensor_type, coordinator)
69  for sensor_type in coordinator.data.sensors
70  )
71 
72 
74  """Representation of a Plaato Sensor."""
75 
76  def __init__(self, data, sensor_type, coordinator=None) -> None:
77  """Initialize plaato sensor."""
78  super().__init__(data, sensor_type, coordinator)
79  if sensor_type is PlaatoKeg.Pins.TEMPERATURE or sensor_type == ATTR_TEMP:
80  self._attr_device_class_attr_device_class = SensorDeviceClass.TEMPERATURE
81 
82  @property
83  def native_value(self):
84  """Return the state of the sensor."""
85  return self._sensor_data_sensor_data.sensors.get(self._sensor_type_sensor_type)
86 
87  @property
89  """Return the unit of measurement."""
90  return self._sensor_data_sensor_data.get_unit_of_measurement(self._sensor_type_sensor_type)
None __init__(self, data, sensor_type, coordinator=None)
Definition: sensor.py:76
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:36
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:42
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193
str|None get_unit_of_measurement(HomeAssistant hass, str entity_id)
Definition: entity.py:184