mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Powerview sensors updates (#80417)
This commit is contained in:
parent
bbe63bca47
commit
bf6efc7589
@ -46,6 +46,9 @@ ROOM_ID = "id"
|
|||||||
SHADE_BATTERY_LEVEL = "batteryStrength"
|
SHADE_BATTERY_LEVEL = "batteryStrength"
|
||||||
SHADE_BATTERY_LEVEL_MAX = 200
|
SHADE_BATTERY_LEVEL_MAX = 200
|
||||||
|
|
||||||
|
ATTR_SIGNAL_STRENGTH = "signalStrength"
|
||||||
|
ATTR_SIGNAL_STRENGTH_MAX = 4
|
||||||
|
|
||||||
STATE_ATTRIBUTE_ROOM_NAME = "roomName"
|
STATE_ATTRIBUTE_ROOM_NAME = "roomName"
|
||||||
|
|
||||||
HUB_EXCEPTIONS = (
|
HUB_EXCEPTIONS = (
|
||||||
|
@ -13,6 +13,8 @@ from homeassistant.helpers.entity import EntityCategory
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
ATTR_SIGNAL_STRENGTH,
|
||||||
|
ATTR_SIGNAL_STRENGTH_MAX,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
ROOM_ID_IN_SHADE,
|
ROOM_ID_IN_SHADE,
|
||||||
ROOM_NAME_UNICODE,
|
ROOM_NAME_UNICODE,
|
||||||
@ -23,58 +25,12 @@ from .entity import ShadeEntity
|
|||||||
from .model import PowerviewEntryData
|
from .model import PowerviewEntryData
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
class PowerViewSensor(ShadeEntity, SensorEntity):
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
||||||
) -> None:
|
|
||||||
"""Set up the hunter douglas shades sensors."""
|
|
||||||
|
|
||||||
pv_entry: PowerviewEntryData = hass.data[DOMAIN][entry.entry_id]
|
|
||||||
|
|
||||||
entities = []
|
|
||||||
for raw_shade in pv_entry.shade_data.values():
|
|
||||||
shade: BaseShade = PvShade(raw_shade, pv_entry.api)
|
|
||||||
if SHADE_BATTERY_LEVEL not in shade.raw_data:
|
|
||||||
continue
|
|
||||||
name_before_refresh = shade.name
|
|
||||||
room_id = shade.raw_data.get(ROOM_ID_IN_SHADE)
|
|
||||||
room_name = pv_entry.room_data.get(room_id, {}).get(ROOM_NAME_UNICODE, "")
|
|
||||||
entities.append(
|
|
||||||
PowerViewShadeBatterySensor(
|
|
||||||
pv_entry.coordinator,
|
|
||||||
pv_entry.device_info,
|
|
||||||
room_name,
|
|
||||||
shade,
|
|
||||||
name_before_refresh,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
async_add_entities(entities)
|
|
||||||
|
|
||||||
|
|
||||||
class PowerViewShadeBatterySensor(ShadeEntity, SensorEntity):
|
|
||||||
"""Representation of an shade battery charge sensor."""
|
"""Representation of an shade battery charge sensor."""
|
||||||
|
|
||||||
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
_attr_entity_category = EntityCategory.DIAGNOSTIC
|
||||||
_attr_native_unit_of_measurement = PERCENTAGE
|
|
||||||
_attr_device_class = SensorDeviceClass.BATTERY
|
|
||||||
_attr_state_class = SensorStateClass.MEASUREMENT
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||||
|
|
||||||
def __init__(self, coordinator, device_info, room_name, shade, name):
|
|
||||||
"""Initialize the shade."""
|
|
||||||
super().__init__(coordinator, device_info, room_name, shade, name)
|
|
||||||
self._attr_unique_id = f"{self._attr_unique_id}_charge"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Name of the shade battery."""
|
|
||||||
return f"{self._shade_name} Battery"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self) -> int:
|
|
||||||
"""Get the current value in percentage."""
|
|
||||||
return round(
|
|
||||||
self._shade.raw_data[SHADE_BATTERY_LEVEL] / SHADE_BATTERY_LEVEL_MAX * 100
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""When entity is added to hass."""
|
"""When entity is added to hass."""
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
@ -87,6 +43,83 @@ class PowerViewShadeBatterySensor(ShadeEntity, SensorEntity):
|
|||||||
self._shade.raw_data = self.data.get_raw_data(self._shade.id)
|
self._shade.raw_data = self.data.get_raw_data(self._shade.id)
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
|
||||||
|
class PowerViewShadeBatterySensor(PowerViewSensor):
|
||||||
|
"""Representation of an shade battery charge sensor."""
|
||||||
|
|
||||||
|
_attr_native_unit_of_measurement = PERCENTAGE
|
||||||
|
_attr_device_class = SensorDeviceClass.BATTERY
|
||||||
|
|
||||||
|
def __init__(self, coordinator, device_info, room_name, shade, name):
|
||||||
|
"""Initialize the shade."""
|
||||||
|
super().__init__(coordinator, device_info, room_name, shade, name)
|
||||||
|
self._attr_unique_id = f"{self._attr_unique_id}_charge"
|
||||||
|
self._attr_name = f"{self._shade_name} Battery"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> int:
|
||||||
|
"""Get the current value in percentage."""
|
||||||
|
return round(
|
||||||
|
self._shade.raw_data[SHADE_BATTERY_LEVEL] / SHADE_BATTERY_LEVEL_MAX * 100
|
||||||
|
)
|
||||||
|
|
||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Refresh shade battery."""
|
"""Refresh shade battery."""
|
||||||
await self._shade.refreshBattery()
|
await self._shade.refresh_battery()
|
||||||
|
|
||||||
|
|
||||||
|
class PowerViewShadeSignalSensor(PowerViewSensor):
|
||||||
|
"""Representation of an shade signal sensor."""
|
||||||
|
|
||||||
|
_attr_native_unit_of_measurement = PERCENTAGE
|
||||||
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
||||||
|
|
||||||
|
def __init__(self, coordinator, device_info, room_name, shade, name):
|
||||||
|
"""Initialize the shade."""
|
||||||
|
super().__init__(coordinator, device_info, room_name, shade, name)
|
||||||
|
self._attr_unique_id = f"{self._attr_unique_id}_signal"
|
||||||
|
self._attr_name = f"{self._shade_name} Signal"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> int:
|
||||||
|
"""Get the current value in percentage."""
|
||||||
|
return round(
|
||||||
|
self._shade.raw_data[ATTR_SIGNAL_STRENGTH] / ATTR_SIGNAL_STRENGTH_MAX * 100
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_update(self) -> None:
|
||||||
|
"""Refresh signal strength."""
|
||||||
|
await self._shade.refresh()
|
||||||
|
|
||||||
|
|
||||||
|
SENSOR_TYPES = {
|
||||||
|
PowerViewShadeBatterySensor: SHADE_BATTERY_LEVEL,
|
||||||
|
PowerViewShadeSignalSensor: ATTR_SIGNAL_STRENGTH,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Set up the hunter douglas shades sensors."""
|
||||||
|
|
||||||
|
pv_entry: PowerviewEntryData = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
|
entities: list[PowerViewSensor] = []
|
||||||
|
for raw_shade in pv_entry.shade_data.values():
|
||||||
|
shade: BaseShade = PvShade(raw_shade, pv_entry.api)
|
||||||
|
name_before_refresh = shade.name
|
||||||
|
room_id = shade.raw_data.get(ROOM_ID_IN_SHADE)
|
||||||
|
room_name = pv_entry.room_data.get(room_id, {}).get(ROOM_NAME_UNICODE, "")
|
||||||
|
for cls, attr in SENSOR_TYPES.items():
|
||||||
|
if attr in shade.raw_data:
|
||||||
|
entities.append(
|
||||||
|
cls(
|
||||||
|
pv_entry.coordinator,
|
||||||
|
pv_entry.device_info,
|
||||||
|
room_name,
|
||||||
|
shade,
|
||||||
|
name_before_refresh,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
async_add_entities(entities)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user