2020-06-02 18:30:00 +02:00
|
|
|
"""Plugwise Binary Sensor component for Home Assistant."""
|
2024-03-08 15:04:07 +01:00
|
|
|
|
2022-02-09 10:15:20 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2023-08-08 16:38:37 +02:00
|
|
|
from collections.abc import Mapping
|
2022-02-09 10:15:20 +01:00
|
|
|
from dataclasses import dataclass
|
2022-02-10 11:17:35 +01:00
|
|
|
from typing import Any
|
2022-02-09 10:15:20 +01:00
|
|
|
|
2023-08-08 16:38:37 +02:00
|
|
|
from plugwise.constants import BinarySensorType
|
2023-05-22 09:19:00 +02:00
|
|
|
|
2022-02-06 17:25:55 +01:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
|
BinarySensorEntity,
|
|
|
|
|
BinarySensorEntityDescription,
|
|
|
|
|
)
|
2023-02-09 20:15:37 +01:00
|
|
|
from homeassistant.const import EntityCategory
|
2022-02-10 11:17:35 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-01-03 11:35:02 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-06-02 18:30:00 +02:00
|
|
|
|
2024-05-13 09:38:06 +02:00
|
|
|
from . import PlugwiseConfigEntry
|
2022-02-06 18:03:50 +01:00
|
|
|
from .coordinator import PlugwiseDataUpdateCoordinator
|
2022-02-05 19:09:37 +01:00
|
|
|
from .entity import PlugwiseEntity
|
2020-06-02 18:30:00 +02:00
|
|
|
|
2020-11-17 02:54:44 +01:00
|
|
|
SEVERITIES = ["other", "info", "warning", "error"]
|
2022-02-09 10:15:20 +01:00
|
|
|
|
|
|
|
|
|
2023-12-19 03:28:13 +01:00
|
|
|
@dataclass(frozen=True)
|
2023-08-08 16:38:37 +02:00
|
|
|
class PlugwiseBinarySensorEntityDescription(BinarySensorEntityDescription):
|
2022-02-09 10:15:20 +01:00
|
|
|
"""Describes a Plugwise binary sensor entity."""
|
|
|
|
|
|
2023-08-08 16:38:37 +02:00
|
|
|
key: BinarySensorType
|
2022-02-09 10:15:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
BINARY_SENSORS: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
|
|
|
|
|
PlugwiseBinarySensorEntityDescription(
|
2022-10-21 10:04:16 +02:00
|
|
|
key="compressor_state",
|
2023-04-03 15:25:02 +02:00
|
|
|
translation_key="compressor_state",
|
2022-10-21 10:04:16 +02:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
|
),
|
|
|
|
|
PlugwiseBinarySensorEntityDescription(
|
2022-11-25 10:55:51 +01:00
|
|
|
key="cooling_enabled",
|
2023-04-03 15:25:02 +02:00
|
|
|
translation_key="cooling_enabled",
|
2022-11-25 10:55:51 +01:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
|
),
|
|
|
|
|
PlugwiseBinarySensorEntityDescription(
|
2022-02-06 17:25:55 +01:00
|
|
|
key="dhw_state",
|
2023-04-03 15:25:02 +02:00
|
|
|
translation_key="dhw_state",
|
2022-02-06 17:25:55 +01:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
|
),
|
2022-02-10 18:09:38 +01:00
|
|
|
PlugwiseBinarySensorEntityDescription(
|
|
|
|
|
key="flame_state",
|
2023-04-03 15:25:02 +02:00
|
|
|
translation_key="flame_state",
|
2022-07-20 11:38:00 +02:00
|
|
|
name="Flame state",
|
2022-02-10 18:09:38 +01:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
|
),
|
2022-02-11 13:36:27 +01:00
|
|
|
PlugwiseBinarySensorEntityDescription(
|
|
|
|
|
key="heating_state",
|
2023-04-03 15:25:02 +02:00
|
|
|
translation_key="heating_state",
|
2022-02-11 13:36:27 +01:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
|
),
|
|
|
|
|
PlugwiseBinarySensorEntityDescription(
|
|
|
|
|
key="cooling_state",
|
2023-04-03 15:25:02 +02:00
|
|
|
translation_key="cooling_state",
|
2022-02-11 13:36:27 +01:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
|
),
|
2022-02-09 10:15:20 +01:00
|
|
|
PlugwiseBinarySensorEntityDescription(
|
2024-04-23 22:13:09 +02:00
|
|
|
key="secondary_boiler_state",
|
|
|
|
|
translation_key="secondary_boiler_state",
|
2022-02-06 17:25:55 +01:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
|
),
|
2022-02-09 18:08:28 +01:00
|
|
|
PlugwiseBinarySensorEntityDescription(
|
|
|
|
|
key="plugwise_notification",
|
2023-04-03 15:25:02 +02:00
|
|
|
translation_key="plugwise_notification",
|
2022-02-09 18:08:28 +01:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
|
),
|
2022-02-06 17:25:55 +01:00
|
|
|
)
|
2020-06-02 18:30:00 +02:00
|
|
|
|
|
|
|
|
|
2022-01-03 11:35:02 +01:00
|
|
|
async def async_setup_entry(
|
|
|
|
|
hass: HomeAssistant,
|
2024-05-13 09:38:06 +02:00
|
|
|
entry: PlugwiseConfigEntry,
|
2022-01-03 11:35:02 +01:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
|
) -> None:
|
2020-06-02 18:30:00 +02:00
|
|
|
"""Set up the Smile binary_sensors from a config entry."""
|
2024-05-13 09:38:06 +02:00
|
|
|
coordinator = entry.runtime_data
|
2020-06-02 18:30:00 +02:00
|
|
|
|
2022-02-08 11:13:05 +01:00
|
|
|
entities: list[PlugwiseBinarySensorEntity] = []
|
2022-02-08 19:08:01 +01:00
|
|
|
for device_id, device in coordinator.data.devices.items():
|
2023-05-22 09:19:00 +02:00
|
|
|
if not (binary_sensors := device.get("binary_sensors")):
|
|
|
|
|
continue
|
2022-02-09 18:08:28 +01:00
|
|
|
for description in BINARY_SENSORS:
|
2023-05-22 09:19:00 +02:00
|
|
|
if description.key not in binary_sensors:
|
2022-02-09 18:08:28 +01:00
|
|
|
continue
|
2020-06-02 18:30:00 +02:00
|
|
|
|
2020-11-17 02:54:44 +01:00
|
|
|
entities.append(
|
2022-02-09 18:08:28 +01:00
|
|
|
PlugwiseBinarySensorEntity(
|
2020-11-17 02:54:44 +01:00
|
|
|
coordinator,
|
2022-02-08 11:13:05 +01:00
|
|
|
device_id,
|
2022-02-09 18:08:28 +01:00
|
|
|
description,
|
2020-11-17 02:54:44 +01:00
|
|
|
)
|
|
|
|
|
)
|
2022-02-08 20:17:49 +01:00
|
|
|
async_add_entities(entities)
|
2020-06-02 18:30:00 +02:00
|
|
|
|
|
|
|
|
|
2022-02-08 11:13:05 +01:00
|
|
|
class PlugwiseBinarySensorEntity(PlugwiseEntity, BinarySensorEntity):
|
2020-11-17 02:54:44 +01:00
|
|
|
"""Represent Smile Binary Sensors."""
|
2020-06-02 18:30:00 +02:00
|
|
|
|
2022-02-09 10:15:20 +01:00
|
|
|
entity_description: PlugwiseBinarySensorEntityDescription
|
|
|
|
|
|
2022-02-05 08:44:31 +01:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
2022-02-06 18:03:50 +01:00
|
|
|
coordinator: PlugwiseDataUpdateCoordinator,
|
2022-02-08 19:08:01 +01:00
|
|
|
device_id: str,
|
2022-02-09 10:15:20 +01:00
|
|
|
description: PlugwiseBinarySensorEntityDescription,
|
2022-02-05 08:44:31 +01:00
|
|
|
) -> None:
|
2020-11-17 02:54:44 +01:00
|
|
|
"""Initialise the binary_sensor."""
|
2022-02-08 19:08:01 +01:00
|
|
|
super().__init__(coordinator, device_id)
|
2022-02-06 17:25:55 +01:00
|
|
|
self.entity_description = description
|
2022-02-08 19:08:01 +01:00
|
|
|
self._attr_unique_id = f"{device_id}-{description.key}"
|
2022-02-10 11:17:35 +01:00
|
|
|
|
|
|
|
|
@property
|
2023-05-22 23:02:42 +02:00
|
|
|
def is_on(self) -> bool:
|
2022-02-10 11:17:35 +01:00
|
|
|
"""Return true if the binary sensor is on."""
|
2023-08-08 16:38:37 +02:00
|
|
|
return self.device["binary_sensors"][self.entity_description.key]
|
2022-02-10 11:17:35 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
|
|
|
|
"""Return entity specific state attributes."""
|
|
|
|
|
if self.entity_description.key != "plugwise_notification":
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
attrs: dict[str, list[str]] = {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"
|
|
|
|
|
attrs[f"{msg_type}_msg"].append(msg)
|
|
|
|
|
|
|
|
|
|
return attrs
|