1 """Support for hunterdouglass_powerview sensors."""
3 from collections.abc
import Callable
4 from dataclasses
import dataclass
5 from typing
import Any, Final
7 from aiopvapi.helpers.constants
import ATTR_NAME
8 from aiopvapi.resources.shade
import BaseShade
13 SensorEntityDescription,
20 from .coordinator
import PowerviewShadeUpdateCoordinator
21 from .entity
import ShadeEntity
22 from .model
import PowerviewConfigEntry, PowerviewDeviceInfo
25 @dataclass(frozen=True)
27 """Mixin to describe a Sensor entity."""
29 update_fn: Callable[[BaseShade], Any]
30 device_class_fn: Callable[[BaseShade], SensorDeviceClass |
None]
31 native_value_fn: Callable[[BaseShade], int]
32 native_unit_fn: Callable[[BaseShade], str |
None]
33 create_entity_fn: Callable[[BaseShade], bool]
36 @dataclass(frozen=True)
38 SensorEntityDescription, PowerviewSensorDescriptionMixin
40 """Class to describe a Sensor entity."""
42 entity_category = EntityCategory.DIAGNOSTIC
43 state_class = SensorStateClass.MEASUREMENT
47 """Get the signal value based on version of API."""
48 return SensorDeviceClass.SIGNAL_STRENGTH
if shade.api_version >= 3
else None
52 """Get the unit of measurement for signal based on version of API."""
53 return SIGNAL_STRENGTH_DECIBELS
if shade.api_version >= 3
else PERCENTAGE
59 device_class_fn=
lambda shade: SensorDeviceClass.BATTERY,
60 native_unit_fn=
lambda shade: PERCENTAGE,
61 native_value_fn=
lambda shade: shade.get_battery_strength(),
62 create_entity_fn=
lambda shade: shade.is_battery_powered(),
63 update_fn=
lambda shade: shade.refresh_battery(suppress_timeout=
True),
67 translation_key=
"signal_strength",
69 device_class_fn=get_signal_device_class,
70 native_unit_fn=get_signal_native_unit,
71 native_value_fn=
lambda shade: shade.get_signal_strength(),
72 create_entity_fn=
lambda shade: shade.has_signal_strength(),
73 update_fn=
lambda shade: shade.refresh(suppress_timeout=
True),
74 entity_registry_enabled_default=
False,
81 entry: PowerviewConfigEntry,
82 async_add_entities: AddEntitiesCallback,
84 """Set up the hunter douglas sensor entities."""
85 pv_entry = entry.runtime_data
86 entities: list[PowerViewSensor] = []
87 for shade
in pv_entry.shade_data.values():
88 room_name = getattr(pv_entry.room_data.get(shade.room_id), ATTR_NAME,
"")
98 for description
in SENSORS
99 if description.create_entity_fn(shade)
105 """Representation of an shade sensor."""
107 entity_description: PowerviewSensorDescription
111 coordinator: PowerviewShadeUpdateCoordinator,
112 device_info: PowerviewDeviceInfo,
116 description: PowerviewSensorDescription,
118 """Initialize the sensor entity."""
119 super().
__init__(coordinator, device_info, room_name, shade, name)
126 """Get the current value of the sensor."""
131 """Return native unit of measurement of sensor."""
136 """Return the class of this entity."""
141 """When entity is added to hass."""
148 """Update with new data from the coordinator."""
153 """Refresh sensor entity."""
154 async
with self.coordinator.radio_operation_lock:
PowerviewShadeData data(self)
None __init__(self, PowerviewShadeUpdateCoordinator coordinator, PowerviewDeviceInfo device_info, str room_name, BaseShade shade, str name, PowerviewSensorDescription description)
None async_added_to_hass(self)
str|None native_unit_of_measurement(self)
SensorDeviceClass|None device_class(self)
None _async_update_shade_from_group(self)
dict[str|int, Any] get_raw_data(self, int shade_id)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
Callable[[], None] async_add_listener(self, CALLBACK_TYPE update_callback, Any context=None)
None async_setup_entry(HomeAssistant hass, PowerviewConfigEntry entry, AddEntitiesCallback async_add_entities)
str get_signal_native_unit(BaseShade shade)
SensorDeviceClass|None get_signal_device_class(BaseShade shade)