BinarySensorEntityDescriptions for Plugwise (#65887)

This commit is contained in:
Franck Nijhof 2022-02-06 17:25:55 +01:00 committed by GitHub
parent 540f1c19d5
commit 62a314015c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 22 deletions

View file

@ -1,9 +1,13 @@
"""Plugwise Binary Sensor component for Home Assistant."""
from plugwise.smile import Smile
from homeassistant.components.binary_sensor import BinarySensorEntity
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 homeassistant.helpers.update_coordinator import DataUpdateCoordinator
@ -20,11 +24,19 @@ from .const import (
)
from .entity import PlugwiseEntity
BINARY_SENSOR_MAP = {
"dhw_state": ["Domestic Hot Water State", None],
"slave_boiler_state": ["Secondary Heater Device State", None],
}
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(
@ -46,8 +58,8 @@ async def async_setup_entry(
if device_properties["class"] == "heater_central":
data = api.get_device_data(dev_id)
for binary_sensor in BINARY_SENSOR_MAP:
if binary_sensor not in data:
for description in BINARY_SENSORS:
if description.key not in data:
continue
entities.append(
@ -56,7 +68,7 @@ async def async_setup_entry(
coordinator,
device_properties["name"],
dev_id,
binary_sensor,
description,
)
)
@ -67,7 +79,10 @@ async def async_setup_entry(
coordinator,
device_properties["name"],
dev_id,
"plugwise_notification",
BinarySensorEntityDescription(
key="plugwise_notification",
name="Plugwise Notification",
),
)
)
@ -83,19 +98,18 @@ class SmileBinarySensor(PlugwiseEntity, BinarySensorEntity):
coordinator: DataUpdateCoordinator,
name: str,
dev_id: str,
binary_sensor: str,
description: BinarySensorEntityDescription,
) -> None:
"""Initialise the binary_sensor."""
super().__init__(api, coordinator, name, dev_id)
self._binary_sensor = binary_sensor
self.entity_description = description
self._attr_is_on = False
self._attr_unique_id = f"{dev_id}-{binary_sensor}"
self._attr_unique_id = f"{dev_id}-{description.key}"
if dev_id == self._api.heater_id:
self._entity_name = "Auxiliary"
sensorname = binary_sensor.replace("_", " ").title()
self._name = f"{self._entity_name} {sensorname}"
self._name = f"{self._entity_name} {description.name}"
if dev_id == self._api.gateway_id:
self._entity_name = f"Smile {self._entity_name}"
@ -113,19 +127,19 @@ class PwBinarySensor(SmileBinarySensor):
def _async_process_data(self) -> None:
"""Update the entity."""
if not (data := self._api.get_device_data(self._dev_id)):
LOGGER.error("Received no data for device %s", self._binary_sensor)
LOGGER.error("Received no data for device %s", self._dev_id)
self.async_write_ha_state()
return
if self._binary_sensor not in data:
if self.entity_description.key not in data:
self.async_write_ha_state()
return
self._attr_is_on = data[self._binary_sensor]
self._attr_is_on = data[self.entity_description.key]
if self._binary_sensor == "dhw_state":
if self.entity_description.key == "dhw_state":
self._attr_icon = FLOW_ON_ICON if self._attr_is_on else FLOW_OFF_ICON
if self._binary_sensor == "slave_boiler_state":
if self.entity_description.key == "slave_boiler_state":
self._attr_icon = FLAME_ICON if self._attr_is_on else IDLE_ICON
self.async_write_ha_state()
@ -140,10 +154,10 @@ class PwNotifySensor(SmileBinarySensor):
coordinator: DataUpdateCoordinator,
name: str,
dev_id: str,
binary_sensor: str,
description: BinarySensorEntityDescription,
) -> None:
"""Set up the Plugwise API."""
super().__init__(api, coordinator, name, dev_id, binary_sensor)
super().__init__(api, coordinator, name, dev_id, description)
self._attr_extra_state_attributes = {}

View file

@ -11,7 +11,7 @@ async def test_anna_climate_binary_sensor_entities(hass, mock_smile_anna):
entry = await async_init_integration(hass, mock_smile_anna)
assert entry.state is ConfigEntryState.LOADED
state = hass.states.get("binary_sensor.auxiliary_slave_boiler_state")
state = hass.states.get("binary_sensor.auxiliary_secondary_boiler_state")
assert str(state.state) == STATE_OFF
state = hass.states.get("binary_sensor.auxiliary_dhw_state")