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