From 6568c387eae3915ee2dbaa7b91015a9e66810d81 Mon Sep 17 00:00:00 2001 From: jimmyd-be Date: Mon, 12 Jun 2023 17:15:38 +0000 Subject: [PATCH] Add binary sensors --- homeassistant/components/renson/__init__.py | 1 + .../components/renson/binary_sensor.py | 133 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 homeassistant/components/renson/binary_sensor.py diff --git a/homeassistant/components/renson/__init__.py b/homeassistant/components/renson/__init__.py index 2e2f4e8f253..9de320f86dd 100644 --- a/homeassistant/components/renson/__init__.py +++ b/homeassistant/components/renson/__init__.py @@ -20,6 +20,7 @@ _LOGGER = logging.getLogger(__name__) PLATFORMS = [ Platform.SENSOR, + Platform.BINARY_SENSOR, ] diff --git a/homeassistant/components/renson/binary_sensor.py b/homeassistant/components/renson/binary_sensor.py new file mode 100644 index 00000000000..0164b838da1 --- /dev/null +++ b/homeassistant/components/renson/binary_sensor.py @@ -0,0 +1,133 @@ +"""Binary sensors for renson.""" +from dataclasses import dataclass + +from renson_endura_delta.field_enum import ( + AIR_QUALITY_CONTROL_FIELD, + BREEZE_ENABLE_FIELD, + BREEZE_MET_FIELD, + CO2_CONTROL_FIELD, + FROST_PROTECTION_FIELD, + HUMIDITY_CONTROL_FIELD, + PREHEATER_FIELD, + DataType, + FieldEnum, +) +from renson_endura_delta.renson import RensonVentilation + +from homeassistant.components.binary_sensor import ( + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import RensonCoordinator +from .const import DOMAIN +from .entity import RensonEntity + + +@dataclass +class RensonBinarySensorEntityDescriptionMixin: + """Mixin for required keys.""" + + field: FieldEnum + + +@dataclass +class RensonBinarySensorEntityDescription( + BinarySensorEntityDescription, RensonBinarySensorEntityDescriptionMixin +): + """Description of binary sensor.""" + + +BINARY_SENSORS: tuple[RensonBinarySensorEntityDescription, ...] = ( + RensonBinarySensorEntityDescription( + name="Frost protection active", + key="FROST_PROTECTION_FIELD", + field=FROST_PROTECTION_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), + RensonBinarySensorEntityDescription( + key="BREEZE_ENABLE_FIELD", + name="Breeze", + field=BREEZE_ENABLE_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), + RensonBinarySensorEntityDescription( + key="BREEZE_MET_FIELD", + name="Breeze conditions met", + field=BREEZE_MET_FIELD, + ), + RensonBinarySensorEntityDescription( + key="HUMIDITY_CONTROL_FIELD", + name="Humidity control", + field=HUMIDITY_CONTROL_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), + RensonBinarySensorEntityDescription( + key="AIR_QUALITY_CONTROL_FIELD", + name="Air quality control", + field=AIR_QUALITY_CONTROL_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), + RensonBinarySensorEntityDescription( + key="CO2_CONTROL_FIELD", + name="CO2 control", + field=CO2_CONTROL_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), + RensonBinarySensorEntityDescription( + key="PREHEATER_FIELD", + name="Preheater", + field=PREHEATER_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), +) + + +class RensonBinarySensor(RensonEntity, BinarySensorEntity): + """Get a sensor data from the Renson API and store it in the state of the class.""" + + def __init__( + self, + description: RensonBinarySensorEntityDescription, + api: RensonVentilation, + coordinator: RensonCoordinator, + ) -> None: + """Initialize class.""" + super().__init__(description.key, api, coordinator) + + self.field = description.field + self.entity_description = description + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + all_data = self.coordinator.data + + value = self.api.get_field_value(all_data, self.field.name) + + self._attr_is_on = self.api.parse_value(value, DataType.BOOLEAN) + + self.async_write_ha_state() + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Call the Renson integration to setup.""" + + api: RensonVentilation = hass.data[DOMAIN][config_entry.entry_id].api + coordinator: RensonCoordinator = hass.data[DOMAIN][ + config_entry.entry_id + ].coordinator + + entities: list = [] + for description in BINARY_SENSORS: + entities.append(RensonBinarySensor(description, api, coordinator)) + + async_add_entities(entities)