More cleanup in Plugwise binary sensor (#66255)

This commit is contained in:
Franck Nijhof 2022-02-10 11:17:35 +01:00 committed by GitHub
parent f5fff95e8b
commit 4cad29d7d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,18 +1,20 @@
"""Plugwise Binary Sensor component for Home Assistant.""" """Plugwise Binary Sensor component for Home Assistant."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription, BinarySensorEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN, LOGGER from .const import DOMAIN
from .coordinator import PlugwiseDataUpdateCoordinator from .coordinator import PlugwiseDataUpdateCoordinator
from .entity import PlugwiseEntity from .entity import PlugwiseEntity
@ -95,35 +97,33 @@ class PlugwiseBinarySensorEntity(PlugwiseEntity, BinarySensorEntity):
super().__init__(coordinator, device_id) super().__init__(coordinator, device_id)
self.entity_description = description self.entity_description = description
self._attr_unique_id = f"{device_id}-{description.key}" self._attr_unique_id = f"{device_id}-{description.key}"
self._attr_name = ( self._attr_name = (f"{self.device.get('name', '')} {description.name}").lstrip()
f"{coordinator.data.devices[device_id].get('name', '')} {description.name}"
).lstrip()
@callback @property
def _handle_coordinator_update(self) -> None: def is_on(self) -> bool | None:
"""Handle updated data from the coordinator.""" """Return true if the binary sensor is on."""
if not (data := self.coordinator.data.devices.get(self._dev_id)): return self.device["binary_sensors"].get(self.entity_description.key)
LOGGER.error("Received no data for device %s", self._dev_id)
super()._handle_coordinator_update()
return
state = data["binary_sensors"].get(self.entity_description.key) @property
self._attr_is_on = state def icon(self) -> str | None:
if icon_off := self.entity_description.icon_off: """Return the icon to use in the frontend, if any."""
self._attr_icon = self.entity_description.icon if state else icon_off if (icon_off := self.entity_description.icon_off) and self.is_on is False:
return icon_off
return self.entity_description.icon
# Add entity attribute for Plugwise notifications @property
if self.entity_description.key == "plugwise_notification": def extra_state_attributes(self) -> Mapping[str, Any] | None:
self._attr_extra_state_attributes = { """Return entity specific state attributes."""
f"{severity}_msg": [] for severity in SEVERITIES if self.entity_description.key != "plugwise_notification":
} return None
if notify := self.coordinator.data.gateway["notifications"]: attrs: dict[str, list[str]] = {f"{severity}_msg": [] for severity in SEVERITIES}
for details in notify.values(): if notify := self.coordinator.data.gateway["notifications"]:
for msg_type, msg in details.items(): for details in notify.values():
msg_type = msg_type.lower() for msg_type, msg in details.items():
if msg_type not in SEVERITIES: msg_type = msg_type.lower()
msg_type = "other" if msg_type not in SEVERITIES:
self._attr_extra_state_attributes[f"{msg_type}_msg"].append(msg) msg_type = "other"
attrs[f"{msg_type}_msg"].append(msg)
super()._handle_coordinator_update() return attrs