Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for myStrom sensors of switches/plugs."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from pymystrom.switch import MyStromSwitch
9 
11  SensorDeviceClass,
12  SensorEntity,
13  SensorEntityDescription,
14  SensorStateClass,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import UnitOfPower, UnitOfTemperature
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.device_registry import DeviceInfo
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from .const import DOMAIN, MANUFACTURER
23 
24 
25 @dataclass(frozen=True)
27  """Class describing mystrom switch sensor entities."""
28 
29  value_fn: Callable[[MyStromSwitch], float | None] = lambda _: None
30 
31 
32 SENSOR_TYPES: tuple[MyStromSwitchSensorEntityDescription, ...] = (
34  key="avg_consumption",
35  translation_key="avg_consumption",
36  device_class=SensorDeviceClass.POWER,
37  native_unit_of_measurement=UnitOfPower.WATT,
38  value_fn=lambda device: device.consumedWs,
39  ),
41  key="consumption",
42  device_class=SensorDeviceClass.POWER,
43  state_class=SensorStateClass.MEASUREMENT,
44  native_unit_of_measurement=UnitOfPower.WATT,
45  value_fn=lambda device: device.consumption,
46  ),
48  key="temperature",
49  device_class=SensorDeviceClass.TEMPERATURE,
50  state_class=SensorStateClass.MEASUREMENT,
51  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
52  value_fn=lambda device: device.temperature,
53  ),
54 )
55 
56 
58  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
59 ) -> None:
60  """Set up the myStrom entities."""
61  device: MyStromSwitch = hass.data[DOMAIN][entry.entry_id].device
62 
64  MyStromSwitchSensor(device, entry.title, description)
65  for description in SENSOR_TYPES
66  if description.value_fn(device) is not None
67  )
68 
69 
71  """Representation of the consumption or temperature of a myStrom switch/plug."""
72 
73  entity_description: MyStromSwitchSensorEntityDescription
74 
75  _attr_has_entity_name = True
76 
77  def __init__(
78  self,
79  device: MyStromSwitch,
80  name: str,
81  description: MyStromSwitchSensorEntityDescription,
82  ) -> None:
83  """Initialize the sensor."""
84  self.devicedevice = device
85  self.entity_descriptionentity_description = description
86 
87  self._attr_unique_id_attr_unique_id = f"{device.mac}-{description.key}"
88  self._attr_device_info_attr_device_info = DeviceInfo(
89  identifiers={(DOMAIN, device.mac)},
90  name=name,
91  manufacturer=MANUFACTURER,
92  sw_version=device.firmware,
93  )
94 
95  @property
96  def native_value(self) -> float | None:
97  """Return the value of the sensor."""
98  return self.entity_descriptionentity_description.value_fn(self.devicedevice)
None __init__(self, MyStromSwitch device, str name, MyStromSwitchSensorEntityDescription description)
Definition: sensor.py:82
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:59