core/homeassistant/components/plugwise/binary_sensor.py

147 lines
4.8 KiB
Python
Raw Normal View History

"""Plugwise Binary Sensor component for Home Assistant."""
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
DOMAIN,
FLAME_ICON,
FLOW_OFF_ICON,
FLOW_ON_ICON,
IDLE_ICON,
LOGGER,
2020-11-17 01:54:44 +00:00
NO_NOTIFICATION_ICON,
NOTIFICATION_ICON,
)
from .coordinator import PlugwiseDataUpdateCoordinator
from .entity import PlugwiseEntity
2020-11-17 01:54:44 +00:00
SEVERITIES = ["other", "info", "warning", "error"]
BINARY_SENSORS: tuple[BinarySensorEntityDescription, ...] = (
BinarySensorEntityDescription(
key="dhw_state",
name="DHW State",
entity_category=EntityCategory.DIAGNOSTIC,
),
BinarySensorEntityDescription(
key="slave_boiler_state",
name="Secondary Boiler State",
entity_category=EntityCategory.DIAGNOSTIC,
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Smile binary_sensors from a config entry."""
coordinator: PlugwiseDataUpdateCoordinator = hass.data[DOMAIN][
config_entry.entry_id
2022-02-08 19:17:49 +00:00
]
2022-02-08 10:13:05 +00:00
entities: list[PlugwiseBinarySensorEntity] = []
for device_id, device in coordinator.data.devices.items():
if device["class"] == "heater_central":
for description in BINARY_SENSORS:
2022-02-08 10:13:05 +00:00
if (
"binary_sensors" not in device
or description.key not in device["binary_sensors"]
2022-02-08 10:13:05 +00:00
):
continue
entities.append(
2022-02-08 10:13:05 +00:00
PlugwiseBinarySensorEntity(
coordinator,
2022-02-08 10:13:05 +00:00
device_id,
description,
)
)
if device["class"] == "gateway":
2020-11-17 01:54:44 +00:00
entities.append(
2022-02-08 10:13:05 +00:00
PlugwiseNotifyBinarySensorEntity(
2020-11-17 01:54:44 +00:00
coordinator,
2022-02-08 10:13:05 +00:00
device_id,
BinarySensorEntityDescription(
key="plugwise_notification",
name="Plugwise Notification",
),
2020-11-17 01:54:44 +00:00
)
)
2022-02-08 19:17:49 +00:00
async_add_entities(entities)
2022-02-08 10:13:05 +00:00
class PlugwiseBinarySensorEntity(PlugwiseEntity, BinarySensorEntity):
2020-11-17 01:54:44 +00:00
"""Represent Smile Binary Sensors."""
def __init__(
self,
coordinator: PlugwiseDataUpdateCoordinator,
device_id: str,
description: BinarySensorEntityDescription,
) -> None:
2020-11-17 01:54:44 +00:00
"""Initialise the binary_sensor."""
super().__init__(coordinator, device_id)
self.entity_description = description
self._attr_is_on = False
self._attr_unique_id = f"{device_id}-{description.key}"
self._attr_name = (
f"{coordinator.data.devices[device_id].get('name', '')} {description.name}"
).lstrip()
2020-11-17 01:54:44 +00:00
@callback
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)):
LOGGER.error("Received no data for device %s", self._dev_id)
super()._handle_coordinator_update()
return
2022-02-08 10:13:05 +00:00
self._attr_is_on = data["binary_sensors"].get(self.entity_description.key)
if self.entity_description.key == "dhw_state":
self._attr_icon = FLOW_ON_ICON if self._attr_is_on else FLOW_OFF_ICON
if self.entity_description.key == "slave_boiler_state":
self._attr_icon = FLAME_ICON if self._attr_is_on else IDLE_ICON
super()._handle_coordinator_update()
2020-11-17 01:54:44 +00:00
2022-02-08 10:13:05 +00:00
class PlugwiseNotifyBinarySensorEntity(PlugwiseBinarySensorEntity):
2020-11-17 01:54:44 +00:00
"""Representation of a Plugwise Notification binary_sensor."""
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
2022-02-08 10:13:05 +00:00
notify = self.coordinator.data.gateway["notifications"]
2020-11-17 01:54:44 +00:00
2022-02-08 10:13:05 +00:00
self._attr_extra_state_attributes = {}
2020-11-17 01:54:44 +00:00
for severity in SEVERITIES:
self._attr_extra_state_attributes[f"{severity}_msg"] = []
2020-11-17 01:54:44 +00:00
self._attr_is_on = False
self._attr_icon = NO_NOTIFICATION_ICON
2020-11-17 01:54:44 +00:00
if notify:
self._attr_is_on = True
self._attr_icon = NOTIFICATION_ICON
2020-11-17 01:54:44 +00:00
for details in notify.values():
for msg_type, msg in details.items():
if msg_type not in SEVERITIES:
msg_type = "other"
self._attr_extra_state_attributes[f"{msg_type.lower()}_msg"].append(
msg
)
2020-11-17 01:54:44 +00:00
self.async_write_ha_state()