Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Avoid warnings until temperature mode is not known for sure #247

Merged
merged 2 commits into from
Sep 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions custom_components/aquarea/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import slugify
from homeassistant.helpers.storage import Store

from homeassistant.components.climate import ClimateEntityDescription
from .definitions import OperatingMode
Expand Down Expand Up @@ -158,7 +159,9 @@ def __init__(
self._sensor_mode = ZoneSensorMode.WATER
self._climate_mode = ZoneClimateMode.DIRECT
self._mode = ZoneTemperatureMode.DIRECT
self.change_mode(ZoneTemperatureMode.DIRECT, initialization=True)
self._mode_guessed = True

self._store = Store(hass, version=1, key=self._attr_unique_id)
# we only display heater by default
self._attr_entity_registry_enabled_default = self.heater

Expand Down Expand Up @@ -231,6 +234,15 @@ def change_mode(self, mode: ZoneTemperatureMode, initialization: bool = False):
# during initialization we cannot write HA state because entities are not registered yet.
# Otherwise it triggers https://github.com/kamaradclimber/heishamon-homeassistant/issues/47
self.async_write_ha_state()
self._mode_guessed = False
self._store.async_delay_save(self.build_data, delay=0)

def build_data(self):
return {
"zone_sensor_mode": int(self._sensor_mode.value),
"zone_climate_mode": int(self._climate_mode.value),
"zone_temperature_mode": int(self._mode.value),
}

async def async_set_temperature(self, **kwargs) -> None:
temperature = kwargs.get("temperature")
Expand Down Expand Up @@ -269,9 +281,18 @@ async def async_set_temperature(self, **kwargs) -> None:

async def async_added_to_hass(self) -> None:
"""Subscribe to MQTT events."""
await super().async_added_to_hass()

stored_values = await self._store.async_load()
if stored_values:
self._sensor_mode = ZoneSensorMode(stored_values["zone_sensor_mode"])
self._climate_mode = ZoneClimateMode(stored_values["zone_climate_mode"])
self._mode = ZoneTemperatureMode(stored_values["zone_temperature_mode"])
self.change_mode(self._mode)
else:
self.change_mode(ZoneTemperatureMode.DIRECT, initialization=True)


async def async_added_to_hass(self) -> None:
"""Subscribe to MQTT events."""
# per zone handle of sensory type to drive mode of operation
@callback
def sensor_mode_received(message):
Expand Down Expand Up @@ -332,10 +353,11 @@ def target_temperature_message_received(message):
_LOGGER.debug(
f"{self._climate_type()} Received target temperature for {self.zone_id}: {self._attr_target_temperature}"
)
if self._attr_min_temp != None and self._attr_max_temp != None:
if self._attr_target_temperature < self._attr_min_temp or self._attr_target_temperature > self._attr_max_temp:
# when reaching that point, maybe we should set a wider range to avoid blocking user?
_LOGGER.warn(f"{self._climate_type()} Target temperature is not within expected range, this is suspicious. {self._attr_target_temperature} should be within [{self._attr_min_temp},{self._attr_max_temp}]")
if not self._mode_guessed:
if self._attr_min_temp != None and self._attr_max_temp != None:
if self._attr_target_temperature < self._attr_min_temp or self._attr_target_temperature > self._attr_max_temp:
# when reaching that point, maybe we should set a wider range to avoid blocking user?
_LOGGER.warn(f"{self._climate_type()} Target temperature is not within expected range, this is suspicious. {self._attr_target_temperature} should be within [{self._attr_min_temp},{self._attr_max_temp}]")
self.async_write_ha_state()

if self.heater:
Expand Down
Loading