BinarySensorEntityDescriptions for Plugwise (#65887)
This commit is contained in:
parent
540f1c19d5
commit
62a314015c
2 changed files with 36 additions and 22 deletions
|
@ -1,9 +1,13 @@
|
||||||
"""Plugwise Binary Sensor component for Home Assistant."""
|
"""Plugwise Binary Sensor component for Home Assistant."""
|
||||||
from plugwise.smile import Smile
|
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.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||||
|
|
||||||
|
@ -20,11 +24,19 @@ from .const import (
|
||||||
)
|
)
|
||||||
from .entity import PlugwiseEntity
|
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"]
|
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(
|
async def async_setup_entry(
|
||||||
|
@ -46,8 +58,8 @@ async def async_setup_entry(
|
||||||
|
|
||||||
if device_properties["class"] == "heater_central":
|
if device_properties["class"] == "heater_central":
|
||||||
data = api.get_device_data(dev_id)
|
data = api.get_device_data(dev_id)
|
||||||
for binary_sensor in BINARY_SENSOR_MAP:
|
for description in BINARY_SENSORS:
|
||||||
if binary_sensor not in data:
|
if description.key not in data:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
entities.append(
|
entities.append(
|
||||||
|
@ -56,7 +68,7 @@ async def async_setup_entry(
|
||||||
coordinator,
|
coordinator,
|
||||||
device_properties["name"],
|
device_properties["name"],
|
||||||
dev_id,
|
dev_id,
|
||||||
binary_sensor,
|
description,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -67,7 +79,10 @@ async def async_setup_entry(
|
||||||
coordinator,
|
coordinator,
|
||||||
device_properties["name"],
|
device_properties["name"],
|
||||||
dev_id,
|
dev_id,
|
||||||
"plugwise_notification",
|
BinarySensorEntityDescription(
|
||||||
|
key="plugwise_notification",
|
||||||
|
name="Plugwise Notification",
|
||||||
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -83,19 +98,18 @@ class SmileBinarySensor(PlugwiseEntity, BinarySensorEntity):
|
||||||
coordinator: DataUpdateCoordinator,
|
coordinator: DataUpdateCoordinator,
|
||||||
name: str,
|
name: str,
|
||||||
dev_id: str,
|
dev_id: str,
|
||||||
binary_sensor: str,
|
description: BinarySensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialise the binary_sensor."""
|
"""Initialise the binary_sensor."""
|
||||||
super().__init__(api, coordinator, name, dev_id)
|
super().__init__(api, coordinator, name, dev_id)
|
||||||
self._binary_sensor = binary_sensor
|
self.entity_description = description
|
||||||
self._attr_is_on = False
|
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:
|
if dev_id == self._api.heater_id:
|
||||||
self._entity_name = "Auxiliary"
|
self._entity_name = "Auxiliary"
|
||||||
|
|
||||||
sensorname = binary_sensor.replace("_", " ").title()
|
self._name = f"{self._entity_name} {description.name}"
|
||||||
self._name = f"{self._entity_name} {sensorname}"
|
|
||||||
|
|
||||||
if dev_id == self._api.gateway_id:
|
if dev_id == self._api.gateway_id:
|
||||||
self._entity_name = f"Smile {self._entity_name}"
|
self._entity_name = f"Smile {self._entity_name}"
|
||||||
|
@ -113,19 +127,19 @@ class PwBinarySensor(SmileBinarySensor):
|
||||||
def _async_process_data(self) -> None:
|
def _async_process_data(self) -> None:
|
||||||
"""Update the entity."""
|
"""Update the entity."""
|
||||||
if not (data := self._api.get_device_data(self._dev_id)):
|
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()
|
self.async_write_ha_state()
|
||||||
return
|
return
|
||||||
|
|
||||||
if self._binary_sensor not in data:
|
if self.entity_description.key not in data:
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
return
|
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
|
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._attr_icon = FLAME_ICON if self._attr_is_on else IDLE_ICON
|
||||||
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
@ -140,10 +154,10 @@ class PwNotifySensor(SmileBinarySensor):
|
||||||
coordinator: DataUpdateCoordinator,
|
coordinator: DataUpdateCoordinator,
|
||||||
name: str,
|
name: str,
|
||||||
dev_id: str,
|
dev_id: str,
|
||||||
binary_sensor: str,
|
description: BinarySensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Plugwise API."""
|
"""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 = {}
|
self._attr_extra_state_attributes = {}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
entry = await async_init_integration(hass, mock_smile_anna)
|
||||||
assert entry.state is ConfigEntryState.LOADED
|
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
|
assert str(state.state) == STATE_OFF
|
||||||
|
|
||||||
state = hass.states.get("binary_sensor.auxiliary_dhw_state")
|
state = hass.states.get("binary_sensor.auxiliary_dhw_state")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue