Skip to content

Commit

Permalink
Allow DemandControl entity to be updated
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kamaradclimber committed Oct 11, 2023
1 parent 0cf53de commit 0c4c262
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 deletions custom_components/aquarea/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down
3 changes: 3 additions & 0 deletions custom_components/aquarea/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 0c4c262

Please sign in to comment.