Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Platform for beewi_smartclim integration."""
2 
3 from __future__ import annotations
4 
5 from beewi_smartclim import BeewiSmartClimPoller
6 import voluptuous as vol
7 
9  PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
10  SensorDeviceClass,
11  SensorEntity,
12 )
13 from homeassistant.const import CONF_MAC, CONF_NAME, PERCENTAGE, UnitOfTemperature
14 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
18 
19 # Default values
20 DEFAULT_NAME = "BeeWi SmartClim"
21 
22 # Sensor config
23 SENSOR_TYPES = [
24  [SensorDeviceClass.TEMPERATURE, "Temperature", UnitOfTemperature.CELSIUS],
25  [SensorDeviceClass.HUMIDITY, "Humidity", PERCENTAGE],
26  [SensorDeviceClass.BATTERY, "Battery", PERCENTAGE],
27 ]
28 
29 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
30  {
31  vol.Required(CONF_MAC): cv.string,
32  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
33  }
34 )
35 
36 
38  hass: HomeAssistant,
39  config: ConfigType,
40  add_entities: AddEntitiesCallback,
41  discovery_info: DiscoveryInfoType | None = None,
42 ) -> None:
43  """Set up the beewi_smartclim platform."""
44 
45  mac = config[CONF_MAC]
46  prefix = config[CONF_NAME]
47  poller = BeewiSmartClimPoller(mac)
48 
49  sensors = []
50 
51  for sensor_type in SENSOR_TYPES:
52  device = sensor_type[0]
53  name = sensor_type[1]
54  unit = sensor_type[2]
55  # `prefix` is the name configured by the user for the sensor, we're appending
56  # the device type at the end of the name (garden -> garden temperature)
57  if prefix:
58  name = f"{prefix} {name}"
59 
60  sensors.append(BeewiSmartclimSensor(poller, name, mac, device, unit))
61 
62  add_entities(sensors)
63 
64 
66  """Representation of a Sensor."""
67 
68  def __init__(self, poller, name, mac, device, unit):
69  """Initialize the sensor."""
70  self._poller_poller = poller
71  self._attr_name_attr_name = name
72  self._device_device = device
73  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = unit
74  self._attr_device_class_attr_device_class = self._device_device
75  self._attr_unique_id_attr_unique_id = f"{mac}_{device}"
76 
77  def update(self) -> None:
78  """Fetch new state data from the poller."""
79  self._poller_poller.update_sensor()
80  self._attr_native_value_attr_native_value = None
81  if self._device_device == SensorDeviceClass.TEMPERATURE:
82  self._attr_native_value_attr_native_value = self._poller_poller.get_temperature()
83  if self._device_device == SensorDeviceClass.HUMIDITY:
84  self._attr_native_value_attr_native_value = self._poller_poller.get_humidity()
85  if self._device_device == SensorDeviceClass.BATTERY:
86  self._attr_native_value_attr_native_value = self._poller_poller.get_battery()
def __init__(self, poller, name, mac, device, unit)
Definition: sensor.py:68
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:42
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40
float|None get_temperature(MadVRCoordinator coordinator, str key)
Definition: sensor.py:57