2020-06-02 16:30:00 +00:00
|
|
|
"""Plugwise Binary Sensor component for Home Assistant."""
|
2022-02-09 09:15:20 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2022-02-06 16:25:55 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorEntity,
|
|
|
|
BinarySensorEntityDescription,
|
|
|
|
)
|
2022-01-03 10:35:02 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-02-06 16:25:55 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2022-01-03 10:35:02 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2022-02-09 17:08:28 +00:00
|
|
|
from .const import DOMAIN, LOGGER
|
2022-02-06 17:03:50 +00:00
|
|
|
from .coordinator import PlugwiseDataUpdateCoordinator
|
2022-02-05 18:09:37 +00:00
|
|
|
from .entity import PlugwiseEntity
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2020-11-17 01:54:44 +00:00
|
|
|
SEVERITIES = ["other", "info", "warning", "error"]
|
2022-02-09 09:15:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class PlugwiseBinarySensorEntityDescription(BinarySensorEntityDescription):
|
|
|
|
"""Describes a Plugwise binary sensor entity."""
|
|
|
|
|
|
|
|
icon_off: str | None = None
|
|
|
|
|
|
|
|
|
|
|
|
BINARY_SENSORS: tuple[PlugwiseBinarySensorEntityDescription, ...] = (
|
|
|
|
PlugwiseBinarySensorEntityDescription(
|
2022-02-06 16:25:55 +00:00
|
|
|
key="dhw_state",
|
|
|
|
name="DHW State",
|
2022-02-09 09:15:20 +00:00
|
|
|
icon="mdi:water-pump",
|
|
|
|
icon_off="mdi:water-pump-off",
|
2022-02-06 16:25:55 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
2022-02-09 09:15:20 +00:00
|
|
|
PlugwiseBinarySensorEntityDescription(
|
2022-02-06 16:25:55 +00:00
|
|
|
key="slave_boiler_state",
|
|
|
|
name="Secondary Boiler State",
|
2022-02-09 09:15:20 +00:00
|
|
|
icon="mdi:fire",
|
|
|
|
icon_off="mdi:circle-off-outline",
|
2022-02-06 16:25:55 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
2022-02-09 17:08:28 +00:00
|
|
|
PlugwiseBinarySensorEntityDescription(
|
|
|
|
key="plugwise_notification",
|
|
|
|
name="Plugwise Notification",
|
|
|
|
icon="mdi:mailbox-up-outline",
|
|
|
|
icon_off="mdi:mailbox-outline",
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
2022-02-06 16:25:55 +00:00
|
|
|
)
|
2020-06-02 16:30:00 +00:00
|
|
|
|
|
|
|
|
2022-01-03 10:35:02 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-06-02 16:30:00 +00:00
|
|
|
"""Set up the Smile binary_sensors from a config entry."""
|
2022-02-06 17:03:50 +00:00
|
|
|
coordinator: PlugwiseDataUpdateCoordinator = hass.data[DOMAIN][
|
|
|
|
config_entry.entry_id
|
2022-02-08 19:17:49 +00:00
|
|
|
]
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2022-02-08 10:13:05 +00:00
|
|
|
entities: list[PlugwiseBinarySensorEntity] = []
|
2022-02-08 18:08:01 +00:00
|
|
|
for device_id, device in coordinator.data.devices.items():
|
2022-02-09 17:08:28 +00:00
|
|
|
for description in BINARY_SENSORS:
|
|
|
|
if (
|
|
|
|
"binary_sensors" not in device
|
|
|
|
or description.key not in device["binary_sensors"]
|
|
|
|
):
|
|
|
|
continue
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2020-11-17 01:54:44 +00:00
|
|
|
entities.append(
|
2022-02-09 17:08:28 +00:00
|
|
|
PlugwiseBinarySensorEntity(
|
2020-11-17 01:54:44 +00:00
|
|
|
coordinator,
|
2022-02-08 10:13:05 +00:00
|
|
|
device_id,
|
2022-02-09 17:08:28 +00:00
|
|
|
description,
|
2020-11-17 01:54:44 +00:00
|
|
|
)
|
|
|
|
)
|
2022-02-08 19:17:49 +00:00
|
|
|
async_add_entities(entities)
|
2020-06-02 16:30:00 +00:00
|
|
|
|
|
|
|
|
2022-02-08 10:13:05 +00:00
|
|
|
class PlugwiseBinarySensorEntity(PlugwiseEntity, BinarySensorEntity):
|
2020-11-17 01:54:44 +00:00
|
|
|
"""Represent Smile Binary Sensors."""
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2022-02-09 09:15:20 +00:00
|
|
|
entity_description: PlugwiseBinarySensorEntityDescription
|
|
|
|
|
2022-02-05 07:44:31 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-02-06 17:03:50 +00:00
|
|
|
coordinator: PlugwiseDataUpdateCoordinator,
|
2022-02-08 18:08:01 +00:00
|
|
|
device_id: str,
|
2022-02-09 09:15:20 +00:00
|
|
|
description: PlugwiseBinarySensorEntityDescription,
|
2022-02-05 07:44:31 +00:00
|
|
|
) -> None:
|
2020-11-17 01:54:44 +00:00
|
|
|
"""Initialise the binary_sensor."""
|
2022-02-08 18:08:01 +00:00
|
|
|
super().__init__(coordinator, device_id)
|
2022-02-06 16:25:55 +00:00
|
|
|
self.entity_description = description
|
2022-02-08 18:08:01 +00:00
|
|
|
self._attr_unique_id = f"{device_id}-{description.key}"
|
|
|
|
self._attr_name = (
|
|
|
|
f"{coordinator.data.devices[device_id].get('name', '')} {description.name}"
|
|
|
|
).lstrip()
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2020-11-17 01:54:44 +00:00
|
|
|
@callback
|
2022-02-08 18:54:10 +00:00
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
2022-02-08 10:13:05 +00:00
|
|
|
if not (data := self.coordinator.data.devices.get(self._dev_id)):
|
2022-02-06 16:25:55 +00:00
|
|
|
LOGGER.error("Received no data for device %s", self._dev_id)
|
2022-02-08 18:54:10 +00:00
|
|
|
super()._handle_coordinator_update()
|
2020-06-02 16:30:00 +00:00
|
|
|
return
|
|
|
|
|
2022-02-09 09:15:20 +00:00
|
|
|
state = data["binary_sensors"].get(self.entity_description.key)
|
|
|
|
self._attr_is_on = state
|
|
|
|
if icon_off := self.entity_description.icon_off:
|
|
|
|
self._attr_icon = self.entity_description.icon if state else icon_off
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2022-02-09 17:08:28 +00:00
|
|
|
# Add entity attribute for Plugwise notifications
|
|
|
|
if self.entity_description.key == "plugwise_notification":
|
|
|
|
self._attr_extra_state_attributes = {
|
|
|
|
f"{severity}_msg": [] for severity in SEVERITIES
|
|
|
|
}
|
2020-11-17 01:54:44 +00:00
|
|
|
|
2022-02-09 17:08:28 +00:00
|
|
|
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"
|
|
|
|
self._attr_extra_state_attributes[f"{msg_type}_msg"].append(msg)
|
2020-11-17 01:54:44 +00:00
|
|
|
|
2022-02-09 17:08:28 +00:00
|
|
|
super()._handle_coordinator_update()
|