From 0c4c26210705a023446f53d533d5e44db991078c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Seux?= Date: Wed, 11 Oct 2023 20:59:33 +0200 Subject: [PATCH] Allow DemandControl entity to be updated This is some sort of hack to compensate for the fact we never receive a "first" value for this entity (and HA does not want to update a number with a slider when it's original value is `None`) Fix #124 --- custom_components/aquarea/definitions.py | 10 ++++++++-- custom_components/aquarea/number.py | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/custom_components/aquarea/definitions.py b/custom_components/aquarea/definitions.py index dea9163..3a2c419 100644 --- a/custom_components/aquarea/definitions.py +++ b/custom_components/aquarea/definitions.py @@ -6,7 +6,7 @@ from collections.abc import Callable from dataclasses import dataclass -from typing import Optional, TypeVar +from typing import Optional, TypeVar, Any import logging from homeassistant.helpers.entity import EntityCategory @@ -229,6 +229,10 @@ class HeishaMonNumberEntityDescription( # function to transform selected option in value sent via mqtt state_to_mqtt: Optional[Callable] = None + # Initial value to set waiting for the first message from MQTT + # if let empty, no value will be set until first message + initial_value: Optional[Any] = None + def positive_to_bool(value: str) -> bool: return int(value) > 0 @@ -244,6 +248,7 @@ def bit_to_bool(value: str) -> Optional[bool]: def read_demandcontrol(value: str) -> Optional[int]: + _LOGGER.warn(f"CHECKPOINT: {value}") i = int(value) if i >= 43 and i <= 234: return int((i - 43) / (234 - 43) * 100) @@ -469,7 +474,7 @@ def build_numbers(mqtt_prefix: str) -> list[HeishaMonNumberEntityDescription]: ), HeishaMonNumberEntityDescription( heishamon_topic_id="SetDemandControl", - key=f"{mqtt_prefix}main/none", # FIXME + key=f"{mqtt_prefix}main/FakeDemandControl", # FIXME: find how to get real value command_topic=f"{mqtt_prefix}commands/SetDemandControl", name="Demand Control", entity_category=EntityCategory.CONFIG, @@ -479,6 +484,7 @@ def build_numbers(mqtt_prefix: str) -> list[HeishaMonNumberEntityDescription]: state=read_demandcontrol, state_to_mqtt=write_demandcontrol, entity_registry_enabled_default=False, # comes from the optional PCB: disabled by default + initial_value=100, ), ] topic_ids = { diff --git a/custom_components/aquarea/number.py b/custom_components/aquarea/number.py index 44741a4..ba57014 100644 --- a/custom_components/aquarea/number.py +++ b/custom_components/aquarea/number.py @@ -58,6 +58,9 @@ def __init__( self._attr_unique_id = ( f"{config_entry.entry_id}-{description.heishamon_topic_id}" ) + if self.entity_description.initial_value is not None: + self._attr_native_value = self.entity_description.initial_value + self.async_write_ha_state() async def async_set_native_value(self, value: float) -> None: _LOGGER.debug(