Refactor Plugwise notification binary sensor (#66159)
This commit is contained in:
parent
aa95150360
commit
d4995624ee
2 changed files with 30 additions and 56 deletions
|
@ -12,7 +12,7 @@ from homeassistant.core import HomeAssistant, callback
|
|||
from homeassistant.helpers.entity import EntityCategory
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN, LOGGER, NO_NOTIFICATION_ICON, NOTIFICATION_ICON
|
||||
from .const import DOMAIN, LOGGER
|
||||
from .coordinator import PlugwiseDataUpdateCoordinator
|
||||
from .entity import PlugwiseEntity
|
||||
|
||||
|
@ -41,6 +41,13 @@ BINARY_SENSORS: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
|
|||
icon_off="mdi:circle-off-outline",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
PlugwiseBinarySensorEntityDescription(
|
||||
key="plugwise_notification",
|
||||
name="Plugwise Notification",
|
||||
icon="mdi:mailbox-up-outline",
|
||||
icon_off="mdi:mailbox-outline",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
@ -56,34 +63,20 @@ async def async_setup_entry(
|
|||
|
||||
entities: list[PlugwiseBinarySensorEntity] = []
|
||||
for device_id, device in coordinator.data.devices.items():
|
||||
if device["class"] == "heater_central":
|
||||
for description in BINARY_SENSORS:
|
||||
if (
|
||||
"binary_sensors" not in device
|
||||
or description.key not in device["binary_sensors"]
|
||||
):
|
||||
continue
|
||||
for description in BINARY_SENSORS:
|
||||
if (
|
||||
"binary_sensors" not in device
|
||||
or description.key not in device["binary_sensors"]
|
||||
):
|
||||
continue
|
||||
|
||||
entities.append(
|
||||
PlugwiseBinarySensorEntity(
|
||||
coordinator,
|
||||
device_id,
|
||||
description,
|
||||
)
|
||||
)
|
||||
|
||||
if device["class"] == "gateway":
|
||||
entities.append(
|
||||
PlugwiseNotifyBinarySensorEntity(
|
||||
PlugwiseBinarySensorEntity(
|
||||
coordinator,
|
||||
device_id,
|
||||
PlugwiseBinarySensorEntityDescription(
|
||||
key="plugwise_notification",
|
||||
name="Plugwise Notification",
|
||||
),
|
||||
description,
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
|
@ -119,35 +112,18 @@ class PlugwiseBinarySensorEntity(PlugwiseEntity, BinarySensorEntity):
|
|||
if icon_off := self.entity_description.icon_off:
|
||||
self._attr_icon = self.entity_description.icon if state else icon_off
|
||||
|
||||
# Add entity attribute for Plugwise notifications
|
||||
if self.entity_description.key == "plugwise_notification":
|
||||
self._attr_extra_state_attributes = {
|
||||
f"{severity}_msg": [] for severity in SEVERITIES
|
||||
}
|
||||
|
||||
if notify := self.coordinator.data.gateway["notifications"]:
|
||||
for details in notify.values():
|
||||
for msg_type, msg in details.items():
|
||||
msg_type = msg_type.lower()
|
||||
if msg_type not in SEVERITIES:
|
||||
msg_type = "other"
|
||||
self._attr_extra_state_attributes[f"{msg_type}_msg"].append(msg)
|
||||
|
||||
super()._handle_coordinator_update()
|
||||
|
||||
|
||||
class PlugwiseNotifyBinarySensorEntity(PlugwiseBinarySensorEntity):
|
||||
"""Representation of a Plugwise Notification binary_sensor."""
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
notify = self.coordinator.data.gateway["notifications"]
|
||||
|
||||
self._attr_extra_state_attributes = {}
|
||||
for severity in SEVERITIES:
|
||||
self._attr_extra_state_attributes[f"{severity}_msg"] = []
|
||||
|
||||
self._attr_is_on = False
|
||||
self._attr_icon = NO_NOTIFICATION_ICON
|
||||
|
||||
if notify:
|
||||
self._attr_is_on = True
|
||||
self._attr_icon = NOTIFICATION_ICON
|
||||
|
||||
for details in notify.values():
|
||||
for msg_type, msg in details.items():
|
||||
if msg_type not in SEVERITIES:
|
||||
msg_type = "other"
|
||||
|
||||
self._attr_extra_state_attributes[f"{msg_type.lower()}_msg"].append(
|
||||
msg
|
||||
)
|
||||
|
||||
self.async_write_ha_state()
|
||||
|
|
|
@ -62,5 +62,3 @@ FLAME_ICON = "mdi:fire"
|
|||
FLOW_OFF_ICON = "mdi:water-pump-off"
|
||||
FLOW_ON_ICON = "mdi:water-pump"
|
||||
IDLE_ICON = "mdi:circle-off-outline"
|
||||
NO_NOTIFICATION_ICON = "mdi:mailbox-outline"
|
||||
NOTIFICATION_ICON = "mdi:mailbox-up-outline"
|
||||
|
|
Loading…
Add table
Reference in a new issue