From 6c45f43c5d2f69c535b2da7108e3cd484e628def Mon Sep 17 00:00:00 2001 From: jimmyd-be <34766203+jimmyd-be@users.noreply.github.com> Date: Mon, 11 Sep 2023 01:24:57 +0200 Subject: [PATCH] Renson number entity (#99358) * Starting number sensor * Filter change config * Add translation to number entity * add number entity to .coveragerc * Moved has_entity_name to description + changed name of entity * Add self.coordinator.async_request_refresh() after changing value * Add device calss and unit of measurement to number entity --- .coveragerc | 1 + homeassistant/components/renson/__init__.py | 1 + homeassistant/components/renson/number.py | 84 ++++++++++++++++++++ homeassistant/components/renson/strings.json | 5 ++ 4 files changed, 91 insertions(+) create mode 100644 homeassistant/components/renson/number.py diff --git a/.coveragerc b/.coveragerc index 23236891807fef..e932be670cb0a5 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1011,6 +1011,7 @@ omit = homeassistant/components/renson/sensor.py homeassistant/components/renson/fan.py homeassistant/components/renson/binary_sensor.py + homeassistant/components/renson/number.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 dbc0468a11adef..7ce143d8a214f1 100644 --- a/homeassistant/components/renson/__init__.py +++ b/homeassistant/components/renson/__init__.py @@ -22,6 +22,7 @@ PLATFORMS = [ Platform.BINARY_SENSOR, Platform.FAN, + Platform.NUMBER, Platform.SENSOR, ] diff --git a/homeassistant/components/renson/number.py b/homeassistant/components/renson/number.py new file mode 100644 index 00000000000000..bf33b75c9e36e8 --- /dev/null +++ b/homeassistant/components/renson/number.py @@ -0,0 +1,84 @@ +"""Platform to control a Renson ventilation unit.""" +from __future__ import annotations + +import logging + +from renson_endura_delta.field_enum import FILTER_PRESET_FIELD, DataType +from renson_endura_delta.renson import RensonVentilation + +from homeassistant.components.number import ( + NumberDeviceClass, + NumberEntity, + NumberEntityDescription, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import EntityCategory, UnitOfTime +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import RensonCoordinator +from .const import DOMAIN +from .entity import RensonEntity + +_LOGGER = logging.getLogger(__name__) + + +RENSON_NUMBER_DESCRIPTION = NumberEntityDescription( + key="filter_change", + translation_key="filter_change", + icon="mdi:filter", + native_step=1, + native_min_value=0, + native_max_value=360, + entity_category=EntityCategory.CONFIG, + has_entity_name=True, + device_class=NumberDeviceClass.DURATION, + native_unit_of_measurement=UnitOfTime.DAYS, +) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the Renson number platform.""" + + api: RensonVentilation = hass.data[DOMAIN][config_entry.entry_id].api + coordinator: RensonCoordinator = hass.data[DOMAIN][ + config_entry.entry_id + ].coordinator + + async_add_entities([RensonNumber(RENSON_NUMBER_DESCRIPTION, api, coordinator)]) + + +class RensonNumber(RensonEntity, NumberEntity): + """Representation of the Renson number platform.""" + + def __init__( + self, + description: NumberEntityDescription, + api: RensonVentilation, + coordinator: RensonCoordinator, + ) -> None: + """Initialize the Renson number.""" + super().__init__(description.key, api, coordinator) + + self.entity_description = description + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._attr_native_value = self.api.parse_value( + self.api.get_field_value(self.coordinator.data, FILTER_PRESET_FIELD.name), + DataType.NUMERIC, + ) + + super()._handle_coordinator_update() + + async def async_set_native_value(self, value: float) -> None: + """Update the current value.""" + + await self.hass.async_add_executor_job(self.api.set_filter_days, value) + + await self.coordinator.async_request_refresh() diff --git a/homeassistant/components/renson/strings.json b/homeassistant/components/renson/strings.json index 20db9e788b8c77..1a4829c2da9dbe 100644 --- a/homeassistant/components/renson/strings.json +++ b/homeassistant/components/renson/strings.json @@ -13,6 +13,11 @@ } }, "entity": { + "number": { + "filter_change": { + "name": "Filter clean/replacement" + } + }, "binary_sensor": { "frost_protection_active": { "name": "Frost protection active"