From 6871271e7328b8cbf1a2c5c6604f292f43780dcd Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 5 Feb 2022 08:44:31 +0100 Subject: [PATCH] Small cleanup in Plugwise binary sensors (#65738) --- .../components/plugwise/binary_sensor.py | 81 ++++++++++--------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/homeassistant/components/plugwise/binary_sensor.py b/homeassistant/components/plugwise/binary_sensor.py index bf72d9edc31..e8bb0f3366a 100644 --- a/homeassistant/components/plugwise/binary_sensor.py +++ b/homeassistant/components/plugwise/binary_sensor.py @@ -1,10 +1,13 @@ """Plugwise Binary Sensor component for Home Assistant.""" import logging +from plugwise.smile import Smile + from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import ( COORDINATOR, @@ -33,8 +36,10 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up the Smile binary_sensors from a config entry.""" - api = hass.data[DOMAIN][config_entry.entry_id]["api"] - coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR] + api: Smile = hass.data[DOMAIN][config_entry.entry_id]["api"] + coordinator: DataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id][ + COORDINATOR + ] entities: list[BinarySensorEntity] = [] is_thermostat = api.single_master_thermostat() @@ -72,17 +77,21 @@ async def async_setup_entry( async_add_entities(entities, True) -class SmileBinarySensor(SmileGateway): +class SmileBinarySensor(SmileGateway, BinarySensorEntity): """Represent Smile Binary Sensors.""" - def __init__(self, api, coordinator, name, dev_id, binary_sensor): + def __init__( + self, + api: Smile, + coordinator: DataUpdateCoordinator, + name: str, + dev_id: str, + binary_sensor: str, + ) -> None: """Initialise the binary_sensor.""" super().__init__(api, coordinator, name, dev_id) - self._binary_sensor = binary_sensor - - self._icon = None - self._is_on = False + self._attr_is_on = False if dev_id == self._api.heater_id: self._entity_name = "Auxiliary" @@ -95,27 +104,17 @@ class SmileBinarySensor(SmileGateway): self._unique_id = f"{dev_id}-{binary_sensor}" - @property - def icon(self): - """Return the icon of this entity.""" - return self._icon - - @property - def is_on(self): - """Return true if the binary sensor is on.""" - return self._is_on - @callback - def _async_process_data(self): + def _async_process_data(self) -> None: """Update the entity.""" raise NotImplementedError -class PwBinarySensor(SmileBinarySensor, BinarySensorEntity): +class PwBinarySensor(SmileBinarySensor): """Representation of a Plugwise binary_sensor.""" @callback - def _async_process_data(self): + 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) @@ -126,50 +125,54 @@ class PwBinarySensor(SmileBinarySensor, BinarySensorEntity): self.async_write_ha_state() return - self._is_on = data[self._binary_sensor] + self._attr_is_on = data[self._binary_sensor] if self._binary_sensor == "dhw_state": - self._icon = FLOW_ON_ICON if self._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": - self._icon = FLAME_ICON if self._is_on else IDLE_ICON + self._attr_icon = FLAME_ICON if self._attr_is_on else IDLE_ICON self.async_write_ha_state() -class PwNotifySensor(SmileBinarySensor, BinarySensorEntity): +class PwNotifySensor(SmileBinarySensor): """Representation of a Plugwise Notification binary_sensor.""" - def __init__(self, api, coordinator, name, dev_id, binary_sensor): + def __init__( + self, + api: Smile, + coordinator: DataUpdateCoordinator, + name: str, + dev_id: str, + binary_sensor: str, + ) -> None: """Set up the Plugwise API.""" super().__init__(api, coordinator, name, dev_id, binary_sensor) - self._attributes = {} - - @property - def extra_state_attributes(self): - """Return the state attributes.""" - return self._attributes + self._attr_extra_state_attributes = {} @callback - def _async_process_data(self): + def _async_process_data(self) -> None: """Update the entity.""" notify = self._api.notifications for severity in SEVERITIES: - self._attributes[f"{severity}_msg"] = [] + self._attr_extra_state_attributes[f"{severity}_msg"] = [] - self._is_on = False - self._icon = NO_NOTIFICATION_ICON + self._attr_is_on = False + self._attr_icon = NO_NOTIFICATION_ICON if notify: - self._is_on = True - self._icon = NOTIFICATION_ICON + self._attr_is_on = True + self._attr_icon = NOTIFICATION_ICON for details in notify.values(): for msg_type, msg in details.items(): if msg_type not in SEVERITIES: msg_type = "other" - self._attributes[f"{msg_type.lower()}_msg"].append(msg) + self._attr_extra_state_attributes[f"{msg_type.lower()}_msg"].append( + msg + ) self.async_write_ha_state()