Skip to content

Commit

Permalink
Plugwise add value_fn for binary_sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
CoMPaTech committed May 16, 2023
1 parent e5c1212 commit 851d83c
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions homeassistant/components/plugwise/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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 DeviceData

from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
Expand All @@ -22,7 +24,16 @@


@dataclass
class PlugwiseBinarySensorEntityDescription(BinarySensorEntityDescription):
class PlugwiseBinarySensorMixin:
"""Mixin for required Plugwise binary sensor base description keys."""

value_fn: Callable[[DeviceData], bool]


@dataclass
class PlugwiseBinarySensorEntityDescription(
BinarySensorEntityDescription, PlugwiseBinarySensorMixin
):
"""Describes a Plugwise binary sensor entity."""

icon_off: str | None = None
Expand All @@ -35,19 +46,22 @@ class PlugwiseBinarySensorEntityDescription(BinarySensorEntityDescription):
icon="mdi:hvac",
icon_off="mdi:hvac-off",
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["binary_sensors"]["compressor_state"],
),
PlugwiseBinarySensorEntityDescription(
key="cooling_enabled",
translation_key="cooling_enabled",
icon="mdi:snowflake-thermometer",
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["binary_sensors"]["cooling_enabled"],
),
PlugwiseBinarySensorEntityDescription(
key="dhw_state",
translation_key="dhw_state",
icon="mdi:water-pump",
icon_off="mdi:water-pump-off",
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["binary_sensors"]["dhw_state"],
),
PlugwiseBinarySensorEntityDescription(
key="flame_state",
Expand All @@ -56,34 +70,39 @@ class PlugwiseBinarySensorEntityDescription(BinarySensorEntityDescription):
icon="mdi:fire",
icon_off="mdi:fire-off",
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["binary_sensors"]["flame_state"],
),
PlugwiseBinarySensorEntityDescription(
key="heating_state",
translation_key="heating_state",
icon="mdi:radiator",
icon_off="mdi:radiator-off",
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["binary_sensors"]["heating_state"],
),
PlugwiseBinarySensorEntityDescription(
key="cooling_state",
translation_key="cooling_state",
icon="mdi:snowflake",
icon_off="mdi:snowflake-off",
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["binary_sensors"]["cooling_state"],
),
PlugwiseBinarySensorEntityDescription(
key="slave_boiler_state",
translation_key="slave_boiler_state",
icon="mdi:fire",
icon_off="mdi:circle-off-outline",
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["binary_sensors"]["slave_boiler_state"],
),
PlugwiseBinarySensorEntityDescription(
key="plugwise_notification",
translation_key="plugwise_notification",
icon="mdi:mailbox-up-outline",
icon_off="mdi:mailbox-outline",
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data["binary_sensors"]["plugwise_notification"],
),
)

Expand All @@ -100,11 +119,10 @@ async def async_setup_entry(

entities: list[PlugwiseBinarySensorEntity] = []
for device_id, device in coordinator.data.devices.items():
if "binary_sensors" not in device:
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 device["binary_sensors"]:
continue

entities.append(
Expand Down Expand Up @@ -136,9 +154,7 @@ def __init__(
@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)

@property
def icon(self) -> str | None:
Expand Down

0 comments on commit 851d83c

Please sign in to comment.