1 """Support for myStrom sensors of switches/plugs."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
8 from pymystrom.switch
import MyStromSwitch
13 SensorEntityDescription,
22 from .const
import DOMAIN, MANUFACTURER
25 @dataclass(frozen=True)
27 """Class describing mystrom switch sensor entities."""
29 value_fn: Callable[[MyStromSwitch], float |
None] =
lambda _:
None
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,
42 device_class=SensorDeviceClass.POWER,
43 state_class=SensorStateClass.MEASUREMENT,
44 native_unit_of_measurement=UnitOfPower.WATT,
45 value_fn=
lambda device: device.consumption,
49 device_class=SensorDeviceClass.TEMPERATURE,
50 state_class=SensorStateClass.MEASUREMENT,
51 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
52 value_fn=
lambda device: device.temperature,
58 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
60 """Set up the myStrom entities."""
61 device: MyStromSwitch = hass.data[DOMAIN][entry.entry_id].device
65 for description
in SENSOR_TYPES
66 if description.value_fn(device)
is not None
71 """Representation of the consumption or temperature of a myStrom switch/plug."""
73 entity_description: MyStromSwitchSensorEntityDescription
75 _attr_has_entity_name =
True
79 device: MyStromSwitch,
81 description: MyStromSwitchSensorEntityDescription,
83 """Initialize the sensor."""
89 identifiers={(DOMAIN, device.mac)},
91 manufacturer=MANUFACTURER,
92 sw_version=device.firmware,
97 """Return the value of the sensor."""
None __init__(self, MyStromSwitch device, str name, MyStromSwitchSensorEntityDescription description)
float|None native_value(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)