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

fix: properly restores preset modes and temps #262

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
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
188 changes: 94 additions & 94 deletions config/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -369,39 +369,39 @@ climate:
# cold_tolerance: 0.3
# hot_tolerance: 0.3

# - platform: dual_smart_thermostat
# name: Edge Case 239
# unique_id: edge_case_239
# heater: switch.heater
# # cooler: switch.cooler
# target_sensor: sensor.room_temp
# heat_cool_mode: false #true # <-important
# keep_alive: #lo attivo e commento initial_hvac_mode per verificare se mantiene lo stato al riavvio
# minutes: 2
# ac_mode: true
# min_temp: 16
# max_temp: 32
# cold_tolerance: 0.4
# hot_tolerance: 0.1
# target_temp_step: 0.1
# min_cycle_duration:
# minutes: 1
# away:
# temperature: 28.0
# target_temp_low: 27
# target_temp_high: 29.5
# home:
# temperature: 23.0
# target_temp_low: 22.5
# target_temp_high: 23.5
# comfort:
# temperature: 25.0
# target_temp_low: 24
# target_temp_high: 25.5
# sleep:
# temperature: 27.5
# target_temp_low: 26.5
# target_temp_high: 28.0
- platform: dual_smart_thermostat
name: Edge Case 239
unique_id: edge_case_239
heater: switch.heater
# cooler: switch.cooler
target_sensor: sensor.room_temp
heat_cool_mode: false #true # <-important
keep_alive: #lo attivo e commento initial_hvac_mode per verificare se mantiene lo stato al riavvio
minutes: 2
ac_mode: true
min_temp: 16
max_temp: 32
cold_tolerance: 0.4
hot_tolerance: 0.1
target_temp_step: 0.1
min_cycle_duration:
minutes: 1
away:
temperature: 28.0
target_temp_low: 27
target_temp_high: 29.5
home:
temperature: 23.0
target_temp_low: 22.5
target_temp_high: 23.5
comfort:
temperature: 25.0
target_temp_low: 24
target_temp_high: 25.5
sleep:
temperature: 27.5
target_temp_low: 26.5
target_temp_high: 28.0

# - platform: dual_smart_thermostat
# name: Edge Case 210
Expand All @@ -428,70 +428,70 @@ climate:
# precision: 0.1
# target_temp_step: 0.5

- platform: dual_smart_thermostat
name: Edge Case 241
unique_id: edge_case_241
heater: switch.heater
cooler: switch.cooler
fan: switch.fan
target_sensor: sensor.room_temp
heat_cool_mode: true # <-required
cold_tolerance: 0.3
hot_tolerance: 0.2
fan_hot_tolerance: 1
target_temp_step: 0.5
min_temp: 14
max_temp: 28
comfort:
temperature: 21
target_temp_low: 21
target_temp_high: 21.5
away:
temperature: 21
target_temp_low: 15
target_temp_high: 28
# - platform: dual_smart_thermostat
# name: Edge Case 241
# unique_id: edge_case_241
# heater: switch.heater
# cooler: switch.cooler
# fan: switch.fan
# target_sensor: sensor.room_temp
# heat_cool_mode: true # <-required
# cold_tolerance: 0.3
# hot_tolerance: 0.2
# fan_hot_tolerance: 1
# target_temp_step: 0.5
# min_temp: 14
# max_temp: 28
# comfort:
# temperature: 21
# target_temp_low: 21
# target_temp_high: 21.5
# away:
# temperature: 21
# target_temp_low: 15
# target_temp_high: 28


- platform: dual_smart_thermostat
name: Dual Humidity
unique_id: dual_humidity
heater: switch.heater
cooler: switch.cooler
dryer: switch.dryer
target_sensor: sensor.room_temp
humidity_sensor: sensor.humidity
heat_cool_mode: true
target_temp_step: 0.1
sensor_stale_duration: "00:10"
precision: 0.1
min_temp: 9
max_temp: 32
target_temp: 20
cold_tolerance: 0.3
hot_tolerance: 0.3
away:
target_temp_high: 30
target_temp_low: 23
humidity: 55
sleep:
target_temp_high: 26
target_temp_low: 18
humidity: 60
# - platform: dual_smart_thermostat
# name: Dual Humidity
# unique_id: dual_humidity
# heater: switch.heater
# cooler: switch.cooler
# dryer: switch.dryer
# target_sensor: sensor.room_temp
# humidity_sensor: sensor.humidity
# heat_cool_mode: true
# target_temp_step: 0.1
# sensor_stale_duration: "00:10"
# precision: 0.1
# min_temp: 9
# max_temp: 32
# target_temp: 20
# cold_tolerance: 0.3
# hot_tolerance: 0.3
# away:
# target_temp_high: 30
# target_temp_low: 23
# humidity: 55
# sleep:
# target_temp_high: 26
# target_temp_low: 18
# humidity: 60

- platform: dual_smart_thermostat
name: Dual Heat Pump
unique_id: dual_heat_pump
heater: switch.heater
target_sensor: sensor.room_temp
heat_pump_cooling: switch.heat_pump_cool
heat_cool_mode: true
target_temp_step: 0.1
precision: 0.1
min_temp: 9
max_temp: 32
target_temp: 20
cold_tolerance: 0.3
hot_tolerance: 0.3
# - platform: dual_smart_thermostat
# name: Dual Heat Pump
# unique_id: dual_heat_pump
# heater: switch.heater
# target_sensor: sensor.room_temp
# heat_pump_cooling: switch.heat_pump_cool
# heat_cool_mode: true
# target_temp_step: 0.1
# precision: 0.1
# min_temp: 9
# max_temp: 32
# target_temp: 20
# cold_tolerance: 0.3
# hot_tolerance: 0.3


# - platform: dual_smart_thermostat
Expand Down
35 changes: 19 additions & 16 deletions custom_components/dual_smart_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
HVACMode,
)
from homeassistant.components.climate.const import (
ATTR_PRESET_MODE,
ATTR_TARGET_TEMP_HIGH,
ATTR_TARGET_TEMP_LOW,
PRESET_NONE,
Expand Down Expand Up @@ -610,24 +609,15 @@ async def _async_startup(*_) -> None:
self._hvac_mode,
)

# restore previous preset mode if available
# TODO: completely handle in peset manager
old_pres_mode = old_state.attributes.get(ATTR_PRESET_MODE)
if self.features.is_range_mode:
if (
self._attr_preset_modes
and old_pres_mode in self.presets.presets_range
):
self._attr_preset_mode = old_pres_mode

elif self._attr_preset_modes and old_pres_mode in self.presets.presets:
_LOGGER.debug("Restoring previous preset mode: %s", old_pres_mode)
self._attr_preset_mode = old_pres_mode
# Set correct support flag as the following actions depend on it
self._set_support_flags()

# restore previous preset mode if available
self.presets.apply_old_state(old_state)
self._attr_preset_mode = self.presets.preset_mode

_LOGGER.debug("restoring hvac_mode: %s", hvac_mode)
await self.async_set_hvac_mode(hvac_mode)
await self.async_set_hvac_mode(hvac_mode, is_restore=True)

self._hvac_action_reason = old_state.attributes.get(ATTR_HVAC_ACTION_REASON)

Expand Down Expand Up @@ -868,7 +858,9 @@ async def _async_update_sensors_initial_state(self) -> bool:

return should_contorl_climate

async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
async def async_set_hvac_mode(
self, hvac_mode: HVACMode, is_restore: bool = False
) -> None:
"""Call climate mode based on current mode."""
_LOGGER.info("Setting hvac mode: %s", hvac_mode)

Expand All @@ -882,6 +874,12 @@ async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:

self._hvac_mode = hvac_mode
self._set_support_flags()

if not is_restore:
self.environment.set_temepratures_from_hvac_mode_and_presets(
self._hvac_mode, self.presets.preset_mode, self.presets.presets_range
)

self._target_humidity = self.environment.target_humidity

await self.hvac_device.async_set_hvac_mode(hvac_mode)
Expand Down Expand Up @@ -1213,6 +1211,11 @@ async def async_set_preset_mode(self, preset_mode: str) -> None:
self.presets.set_preset_mode(preset_mode, self.hvac_device.hvac_mode)

self._attr_preset_mode = self.presets.preset_mode

self.environment.set_temepratures_from_hvac_mode_and_presets(
self._hvac_mode, preset_mode, self.presets.presets_range
)

await self._async_control_climate(force=True)
self.async_write_ha_state()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,11 @@ def _set_default_temps_range_mode(self) -> None:
self._target_temp_high += PRECISION_WHOLE

def set_temepratures_from_hvac_mode_and_presets(
self, hvac_mode: HVACMode, preset_mode: str, presets_range: dict[str, Any]
self,
hvac_mode: HVACMode,
preset_mode: str,
presets_range: dict[str, Any],
old_preset_mode: str | None = None,
) -> None:
if preset_mode is None or preset_mode is PRESET_NONE:
return
Expand All @@ -558,13 +562,34 @@ def set_temepratures_from_hvac_mode_and_presets(
presets_range,
)

if hvac_mode == HVACMode.HEAT and presets_range[preset_mode][0] is not None:
if (
hvac_mode == HVACMode.HEAT
and preset_mode in presets_range
and presets_range[preset_mode][0] is not None
):
_LOGGER.debug(
"HVACMode.HEAT Setting target temp from preset: %s",
presets_range[preset_mode][0],
)
self._target_temp = presets_range[preset_mode][0]
elif (
hvac_mode in [HVACMode.COOL, HVACMode.FAN_ONLY]
and preset_mode in presets_range
and presets_range[preset_mode][0] is not None
):
self._target_temp = presets_range[preset_mode][1]
_LOGGER.debug(
"HVACMode.COOL, HVACMode.FAN_ONLY Setting target temp from preset: %s, sved_target_temp: %s",
presets_range[preset_mode][1],
self._saved_target_temp,
)
preset_match_old = old_preset_mode == preset_mode
self._target_temp = (
self._saved_target_temp
if preset_match_old and self._saved_target_temp
else presets_range[preset_mode][1]
)
else:
_LOGGER.debug("Setting target temp from preset, unhandled case")

def apply_old_state(self, old_state: State) -> None:
_LOGGER.debug("Applying old state: %s", old_state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,6 @@ def set_support_flags(
)
self._supported_features |= ClimateEntityFeature.PRESET_MODE

self.environment.set_temepratures_from_hvac_mode_and_presets(
current_hvac_mode, preset_mode, presets_range
)

elif current_hvac_mode == HVACMode.DRY:
self._supported_features = (
self._default_support_flags | ClimateEntityFeature.TARGET_HUMIDITY
Expand Down
Loading