From 9e8d89c4f54127508cdddfae5b11f1eb8f0f8a8c Mon Sep 17 00:00:00 2001 From: jimmyd-be <34766203+jimmyd-be@users.noreply.github.com> Date: Mon, 28 Aug 2023 21:15:18 +0200 Subject: [PATCH] Renson binary sensors (#94490) * Add binary sensors * Add Renson services * Add fan to Renson * Revert "Add fan to Renson" This reverts commit 8e7c09671ebf0a53ce0bb633d5209a3add2856b6. * Revert "Add Renson services" This reverts commit d862976c81404623eccd64275f412dc9cdb2c1c4. * Add binary sensor to coveragerc file * Update homeassistant/components/renson/binary_sensor.py Co-authored-by: Erik Montnemery * Update homeassistant/components/renson/binary_sensor.py Co-authored-by: Erik Montnemery * Changed hard coded names to use translation * Code cleaning * Use super()._handle_coordinator_update() --------- Co-authored-by: Erik Montnemery --- .coveragerc | 1 + homeassistant/components/renson/__init__.py | 1 + .../components/renson/binary_sensor.py | 136 ++++++++++++++++++ homeassistant/components/renson/strings.json | 23 +++ 4 files changed, 161 insertions(+) create mode 100644 homeassistant/components/renson/binary_sensor.py diff --git a/.coveragerc b/.coveragerc index 6f26795d1b5..97ed97ef293 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1006,6 +1006,7 @@ omit = homeassistant/components/renson/const.py homeassistant/components/renson/entity.py homeassistant/components/renson/sensor.py + homeassistant/components/renson/binary_sensor.py homeassistant/components/raspyrfm/* homeassistant/components/recollect_waste/sensor.py homeassistant/components/recorder/repack.py diff --git a/homeassistant/components/renson/__init__.py b/homeassistant/components/renson/__init__.py index bac9bafa8a5..86dfdc1f18b 100644 --- a/homeassistant/components/renson/__init__.py +++ b/homeassistant/components/renson/__init__.py @@ -20,6 +20,7 @@ from .const import DOMAIN _LOGGER = logging.getLogger(__name__) PLATFORMS = [ + Platform.BINARY_SENSOR, Platform.SENSOR, ] diff --git a/homeassistant/components/renson/binary_sensor.py b/homeassistant/components/renson/binary_sensor.py new file mode 100644 index 00000000000..cad8b92c0c3 --- /dev/null +++ b/homeassistant/components/renson/binary_sensor.py @@ -0,0 +1,136 @@ +"""Binary sensors for renson.""" +from __future__ import annotations + +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( + translation_key="frost_protection_active", + key="FROST_PROTECTION_FIELD", + field=FROST_PROTECTION_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), + RensonBinarySensorEntityDescription( + key="BREEZE_ENABLE_FIELD", + translation_key="breeze", + field=BREEZE_ENABLE_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), + RensonBinarySensorEntityDescription( + key="BREEZE_MET_FIELD", + translation_key="breeze_conditions_met", + field=BREEZE_MET_FIELD, + ), + RensonBinarySensorEntityDescription( + key="HUMIDITY_CONTROL_FIELD", + translation_key="humidity_control", + field=HUMIDITY_CONTROL_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), + RensonBinarySensorEntityDescription( + key="AIR_QUALITY_CONTROL_FIELD", + translation_key="air_quality_control", + field=AIR_QUALITY_CONTROL_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), + RensonBinarySensorEntityDescription( + key="CO2_CONTROL_FIELD", + translation_key="co2_control", + field=CO2_CONTROL_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), + RensonBinarySensorEntityDescription( + key="PREHEATER_FIELD", + translation_key="preheater", + field=PREHEATER_FIELD, + entity_category=EntityCategory.DIAGNOSTIC, + ), +) + + +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 + + async_add_entities( + RensonBinarySensor(description, api, coordinator) + for description in BINARY_SENSORS + ) + + +class RensonBinarySensor(RensonEntity, BinarySensorEntity): + """Get sensor data from the Renson API and store it in the state of the class.""" + + _attr_has_entity_name = True + + 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) + + super()._handle_coordinator_update() diff --git a/homeassistant/components/renson/strings.json b/homeassistant/components/renson/strings.json index 06636c9d503..20db9e788b8 100644 --- a/homeassistant/components/renson/strings.json +++ b/homeassistant/components/renson/strings.json @@ -13,6 +13,29 @@ } }, "entity": { + "binary_sensor": { + "frost_protection_active": { + "name": "Frost protection active" + }, + "breeze": { + "name": "Breeze" + }, + "breeze_conditions_met": { + "name": "Breeze conditions met" + }, + "humidity_control": { + "name": "Humidity control" + }, + "air_quality_control": { + "name": "Air quality control" + }, + "co2_control": { + "name": "CO2 control" + }, + "preheater": { + "name": "Preheater" + } + }, "sensor": { "co2_quality_category": { "name": "CO2 quality category",