2020-06-02 16:30:00 +00:00
|
|
|
"""Plugwise Binary Sensor component for Home Assistant."""
|
2022-02-05 07:44:31 +00:00
|
|
|
from plugwise.smile import Smile
|
|
|
|
|
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
|
|
|
|
2020-09-06 20:26:14 +00:00
|
|
|
from .const import (
|
|
|
|
COORDINATOR,
|
|
|
|
DOMAIN,
|
|
|
|
FLAME_ICON,
|
|
|
|
FLOW_OFF_ICON,
|
|
|
|
FLOW_ON_ICON,
|
|
|
|
IDLE_ICON,
|
2022-02-05 23:43:05 +00:00
|
|
|
LOGGER,
|
2020-11-17 01:54:44 +00:00
|
|
|
NO_NOTIFICATION_ICON,
|
|
|
|
NOTIFICATION_ICON,
|
2020-09-06 20:26:14 +00:00
|
|
|
)
|
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-06 16:25:55 +00:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
)
|
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-05 07:44:31 +00:00
|
|
|
api: Smile = hass.data[DOMAIN][config_entry.entry_id]["api"]
|
2022-02-06 17:03:50 +00:00
|
|
|
coordinator: PlugwiseDataUpdateCoordinator = hass.data[DOMAIN][
|
|
|
|
config_entry.entry_id
|
|
|
|
][COORDINATOR]
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2022-01-20 09:14:52 +00:00
|
|
|
entities: list[BinarySensorEntity] = []
|
2020-11-17 01:54:44 +00:00
|
|
|
is_thermostat = api.single_master_thermostat()
|
2020-06-02 16:30:00 +00:00
|
|
|
|
|
|
|
all_devices = api.get_all_devices()
|
|
|
|
for dev_id, device_properties in all_devices.items():
|
2020-11-17 01:54:44 +00:00
|
|
|
|
2020-11-08 17:09:43 +00:00
|
|
|
if device_properties["class"] == "heater_central":
|
|
|
|
data = api.get_device_data(dev_id)
|
2022-02-06 16:25:55 +00:00
|
|
|
for description in BINARY_SENSORS:
|
|
|
|
if description.key not in data:
|
2020-11-08 17:09:43 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
entities.append(
|
|
|
|
PwBinarySensor(
|
|
|
|
api,
|
|
|
|
coordinator,
|
|
|
|
device_properties["name"],
|
|
|
|
dev_id,
|
2022-02-06 16:25:55 +00:00
|
|
|
description,
|
2020-11-08 17:09:43 +00:00
|
|
|
)
|
2020-06-04 06:18:46 +00:00
|
|
|
)
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2020-11-17 01:54:44 +00:00
|
|
|
if device_properties["class"] == "gateway" and is_thermostat is not None:
|
|
|
|
entities.append(
|
|
|
|
PwNotifySensor(
|
|
|
|
api,
|
|
|
|
coordinator,
|
|
|
|
device_properties["name"],
|
|
|
|
dev_id,
|
2022-02-06 16:25:55 +00:00
|
|
|
BinarySensorEntityDescription(
|
|
|
|
key="plugwise_notification",
|
|
|
|
name="Plugwise Notification",
|
|
|
|
),
|
2020-11-17 01:54:44 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-06-02 16:30:00 +00:00
|
|
|
async_add_entities(entities, True)
|
|
|
|
|
|
|
|
|
2022-02-05 18:09:37 +00:00
|
|
|
class SmileBinarySensor(PlugwiseEntity, BinarySensorEntity):
|
2020-11-17 01:54:44 +00:00
|
|
|
"""Represent Smile Binary Sensors."""
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2022-02-05 07:44:31 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
api: Smile,
|
2022-02-06 17:03:50 +00:00
|
|
|
coordinator: PlugwiseDataUpdateCoordinator,
|
2022-02-05 07:44:31 +00:00
|
|
|
name: str,
|
|
|
|
dev_id: str,
|
2022-02-06 16:25:55 +00:00
|
|
|
description: BinarySensorEntityDescription,
|
2022-02-05 07:44:31 +00:00
|
|
|
) -> None:
|
2020-11-17 01:54:44 +00:00
|
|
|
"""Initialise the binary_sensor."""
|
|
|
|
super().__init__(api, coordinator, name, dev_id)
|
2022-02-06 16:25:55 +00:00
|
|
|
self.entity_description = description
|
2022-02-05 07:44:31 +00:00
|
|
|
self._attr_is_on = False
|
2022-02-06 16:25:55 +00:00
|
|
|
self._attr_unique_id = f"{dev_id}-{description.key}"
|
2020-11-17 01:54:44 +00:00
|
|
|
|
|
|
|
if dev_id == self._api.heater_id:
|
|
|
|
self._entity_name = "Auxiliary"
|
|
|
|
|
2022-02-06 16:25:55 +00:00
|
|
|
self._name = f"{self._entity_name} {description.name}"
|
2020-11-17 01:54:44 +00:00
|
|
|
|
|
|
|
if dev_id == self._api.gateway_id:
|
|
|
|
self._entity_name = f"Smile {self._entity_name}"
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2020-11-17 01:54:44 +00:00
|
|
|
@callback
|
2022-02-05 07:44:31 +00:00
|
|
|
def _async_process_data(self) -> None:
|
2020-11-17 01:54:44 +00:00
|
|
|
"""Update the entity."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2022-02-05 07:44:31 +00:00
|
|
|
class PwBinarySensor(SmileBinarySensor):
|
2020-11-17 01:54:44 +00:00
|
|
|
"""Representation of a Plugwise binary_sensor."""
|
|
|
|
|
2020-06-02 16:30:00 +00:00
|
|
|
@callback
|
2022-02-05 07:44:31 +00:00
|
|
|
def _async_process_data(self) -> None:
|
2020-06-02 16:30:00 +00:00
|
|
|
"""Update the entity."""
|
2021-10-31 17:32:17 +00:00
|
|
|
if not (data := self._api.get_device_data(self._dev_id)):
|
2022-02-06 16:25:55 +00:00
|
|
|
LOGGER.error("Received no data for device %s", self._dev_id)
|
2020-06-02 16:30:00 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
return
|
|
|
|
|
2022-02-06 16:25:55 +00:00
|
|
|
if self.entity_description.key not in data:
|
2020-06-04 06:18:46 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
return
|
|
|
|
|
2022-02-06 16:25:55 +00:00
|
|
|
self._attr_is_on = data[self.entity_description.key]
|
2020-06-02 16:30:00 +00:00
|
|
|
|
2022-02-06 16:25:55 +00:00
|
|
|
if self.entity_description.key == "dhw_state":
|
2022-02-05 07:44:31 +00:00
|
|
|
self._attr_icon = FLOW_ON_ICON if self._attr_is_on else FLOW_OFF_ICON
|
2022-02-06 16:25:55 +00:00
|
|
|
if self.entity_description.key == "slave_boiler_state":
|
2022-02-05 07:44:31 +00:00
|
|
|
self._attr_icon = FLAME_ICON if self._attr_is_on else IDLE_ICON
|
2020-06-02 16:30:00 +00:00
|
|
|
|
|
|
|
self.async_write_ha_state()
|
2020-11-17 01:54:44 +00:00
|
|
|
|
|
|
|
|
2022-02-05 07:44:31 +00:00
|
|
|
class PwNotifySensor(SmileBinarySensor):
|
2020-11-17 01:54:44 +00:00
|
|
|
"""Representation of a Plugwise Notification binary_sensor."""
|
|
|
|
|
2022-02-05 07:44:31 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
api: Smile,
|
2022-02-06 17:03:50 +00:00
|
|
|
coordinator: PlugwiseDataUpdateCoordinator,
|
2022-02-05 07:44:31 +00:00
|
|
|
name: str,
|
|
|
|
dev_id: str,
|
2022-02-06 16:25:55 +00:00
|
|
|
description: BinarySensorEntityDescription,
|
2022-02-05 07:44:31 +00:00
|
|
|
) -> None:
|
2020-11-17 01:54:44 +00:00
|
|
|
"""Set up the Plugwise API."""
|
2022-02-06 16:25:55 +00:00
|
|
|
super().__init__(api, coordinator, name, dev_id, description)
|
2020-11-17 01:54:44 +00:00
|
|
|
|
2022-02-05 07:44:31 +00:00
|
|
|
self._attr_extra_state_attributes = {}
|
2020-11-17 01:54:44 +00:00
|
|
|
|
|
|
|
@callback
|
2022-02-05 07:44:31 +00:00
|
|
|
def _async_process_data(self) -> None:
|
2020-11-17 01:54:44 +00:00
|
|
|
"""Update the entity."""
|
|
|
|
notify = self._api.notifications
|
|
|
|
|
|
|
|
for severity in SEVERITIES:
|
2022-02-05 07:44:31 +00:00
|
|
|
self._attr_extra_state_attributes[f"{severity}_msg"] = []
|
2020-11-17 01:54:44 +00:00
|
|
|
|
2022-02-05 07:44:31 +00:00
|
|
|
self._attr_is_on = False
|
|
|
|
self._attr_icon = NO_NOTIFICATION_ICON
|
2020-11-17 01:54:44 +00:00
|
|
|
|
|
|
|
if notify:
|
2022-02-05 07:44:31 +00:00
|
|
|
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"
|
|
|
|
|
2022-02-05 07:44:31 +00:00
|
|
|
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()
|