Plugwise prepare typing for binary_sensor (#93162)

This commit is contained in:
Tom 2023-05-22 09:19:00 +02:00 committed by GitHub
parent aa937b1640
commit 79cafd5a46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,10 +1,12 @@
"""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 collections.abc import Callable, Mapping
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any from typing import Any
from plugwise import SmileBinarySensors
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription, BinarySensorEntityDescription,
@ -22,7 +24,16 @@ SEVERITIES = ["other", "info", "warning", "error"]
@dataclass @dataclass
class PlugwiseBinarySensorEntityDescription(BinarySensorEntityDescription): class PlugwiseBinarySensorMixin:
"""Mixin for required Plugwise binary sensor base description keys."""
value_fn: Callable[[SmileBinarySensors], bool]
@dataclass
class PlugwiseBinarySensorEntityDescription(
BinarySensorEntityDescription, PlugwiseBinarySensorMixin
):
"""Describes a Plugwise binary sensor entity.""" """Describes a Plugwise binary sensor entity."""
icon_off: str | None = None icon_off: str | None = None
@ -35,12 +46,14 @@ BINARY_SENSORS: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
icon="mdi:hvac", icon="mdi:hvac",
icon_off="mdi:hvac-off", icon_off="mdi:hvac-off",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["compressor_state"],
), ),
PlugwiseBinarySensorEntityDescription( PlugwiseBinarySensorEntityDescription(
key="cooling_enabled", key="cooling_enabled",
translation_key="cooling_enabled", translation_key="cooling_enabled",
icon="mdi:snowflake-thermometer", icon="mdi:snowflake-thermometer",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["cooling_enabled"],
), ),
PlugwiseBinarySensorEntityDescription( PlugwiseBinarySensorEntityDescription(
key="dhw_state", key="dhw_state",
@ -48,6 +61,7 @@ BINARY_SENSORS: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
icon="mdi:water-pump", icon="mdi:water-pump",
icon_off="mdi:water-pump-off", icon_off="mdi:water-pump-off",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["dhw_state"],
), ),
PlugwiseBinarySensorEntityDescription( PlugwiseBinarySensorEntityDescription(
key="flame_state", key="flame_state",
@ -56,6 +70,7 @@ BINARY_SENSORS: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
icon="mdi:fire", icon="mdi:fire",
icon_off="mdi:fire-off", icon_off="mdi:fire-off",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["flame_state"],
), ),
PlugwiseBinarySensorEntityDescription( PlugwiseBinarySensorEntityDescription(
key="heating_state", key="heating_state",
@ -63,6 +78,7 @@ BINARY_SENSORS: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
icon="mdi:radiator", icon="mdi:radiator",
icon_off="mdi:radiator-off", icon_off="mdi:radiator-off",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["heating_state"],
), ),
PlugwiseBinarySensorEntityDescription( PlugwiseBinarySensorEntityDescription(
key="cooling_state", key="cooling_state",
@ -70,6 +86,7 @@ BINARY_SENSORS: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
icon="mdi:snowflake", icon="mdi:snowflake",
icon_off="mdi:snowflake-off", icon_off="mdi:snowflake-off",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["cooling_state"],
), ),
PlugwiseBinarySensorEntityDescription( PlugwiseBinarySensorEntityDescription(
key="slave_boiler_state", key="slave_boiler_state",
@ -77,6 +94,7 @@ BINARY_SENSORS: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
icon="mdi:fire", icon="mdi:fire",
icon_off="mdi:circle-off-outline", icon_off="mdi:circle-off-outline",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["slave_boiler_state"],
), ),
PlugwiseBinarySensorEntityDescription( PlugwiseBinarySensorEntityDescription(
key="plugwise_notification", key="plugwise_notification",
@ -84,6 +102,7 @@ BINARY_SENSORS: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
icon="mdi:mailbox-up-outline", icon="mdi:mailbox-up-outline",
icon_off="mdi:mailbox-outline", icon_off="mdi:mailbox-outline",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["plugwise_notification"],
), ),
) )
@ -100,11 +119,10 @@ async def async_setup_entry(
entities: list[PlugwiseBinarySensorEntity] = [] entities: list[PlugwiseBinarySensorEntity] = []
for device_id, device in coordinator.data.devices.items(): for device_id, device in coordinator.data.devices.items():
if not (binary_sensors := device.get("binary_sensors")):
continue
for description in BINARY_SENSORS: for description in BINARY_SENSORS:
if description.key not in device and ( if description.key not in binary_sensors:
"binary_sensors" not in device
or description.key not in device["binary_sensors"]
):
continue continue
entities.append( entities.append(
@ -136,9 +154,7 @@ class PlugwiseBinarySensorEntity(PlugwiseEntity, BinarySensorEntity):
@property @property
def is_on(self) -> bool | None: def is_on(self) -> bool | None:
"""Return true if the binary sensor is on.""" """Return true if the binary sensor is on."""
if self.entity_description.key in self.device: return self.entity_description.value_fn(self.device["binary_sensors"])
return self.device[self.entity_description.key]
return self.device["binary_sensors"].get(self.entity_description.key)
@property @property
def icon(self) -> str | None: def icon(self) -> str | None: