Skip to content

Commit

Permalink
fix: preset mode not awailable
Browse files Browse the repository at this point in the history
Fixes #80
  • Loading branch information
= authored and swingerman committed Mar 21, 2024
1 parent 80ddb3b commit f8b4adc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
14 changes: 13 additions & 1 deletion custom_components/dual_smart_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,21 @@ async def async_setup_platform(
presets_dict = {
key: config[value] for key, value in CONF_PRESETS.items() if value in config
}
_LOGGER.debug("Presets dict: %s", presets_dict)
presets = {
key: values[ATTR_TEMPERATURE]
for key, values in presets_dict.items()
if ATTR_TEMPERATURE in values
}
_LOGGER.debug("Presets: %s", presets)
presets_range = {
key: [values[ATTR_TARGET_TEMP_LOW], values[ATTR_TARGET_TEMP_HIGH]]
for key, values in presets_dict.items()
if ATTR_TARGET_TEMP_LOW in values
and ATTR_TARGET_TEMP_HIGH in values
and values[ATTR_TARGET_TEMP_LOW] < values[ATTR_TARGET_TEMP_HIGH]
}
_LOGGER.debug("Presets range: %s", presets_range)

# Try to load presets in old format and use if new format not available in config
old_presets = {k: config[v] for k, v in CONF_PRESETS_OLD.items() if v in config}
Expand Down Expand Up @@ -401,11 +404,20 @@ def __init__(
_LOGGER.debug("INIT - Setting support flag: presets - no presets set")
self._presets = presets
if len(presets_range):
_LOGGER.debug(
"INIT - Setting support flag: presets range: %s", presets_range
)
self._preset_range_modes = [PRESET_NONE] + list(presets_range.keys())
else:
_LOGGER.debug("INIT - Setting support flag: presets range none")
self._preset_range_modes = [PRESET_NONE]
self._presets_range = presets_range
self._preset_mode = PRESET_NONE

if self._preset_range_modes:
# if range mode is enabled, we need to add the range presets to the preset modes avoiding duplicates
self._attr_preset_modes = self._attr_preset_modes + list(
set(self._preset_range_modes) - set(self._attr_preset_modes)
)

# aux heater last run time
if self.aux_heater_entity_id and self.aux_heater_timeout:
Expand Down
45 changes: 44 additions & 1 deletion tests/test_dual_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
PRESET_SLEEP,
HVACMode,
)
from homeassistant.components.climate.const import DOMAIN as CLIMATE
from homeassistant.components.climate.const import ATTR_PRESET_MODE, DOMAIN as CLIMATE
from homeassistant.const import ENTITY_MATCH_ALL, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
Expand Down Expand Up @@ -144,6 +144,49 @@ async def test_setup_gets_current_temp_from_sensor(
assert hass.states.get(common.ENTITY).attributes["current_temperature"] == 18


async def test_use_case_1(
hass: HomeAssistant,
) -> None: # noqa: F811
"""Test that current temperature is updated on entity addition."""
hass.config.units = METRIC_SYSTEM
setup_sensor(hass, 18)
await hass.async_block_till_done()

assert await async_setup_component(
hass,
CLIMATE,
{
"climate": {
"platform": DOMAIN,
"name": "test",
"heater": common.ENT_HEATER,
"cooler": common.ENT_COOLER,
"target_sensor": common.ENT_SENSOR,
"min_cycle_duration": timedelta(seconds=60),
"precision": 0.5,
"min_temp": 20,
"max_temp": 25,
"heat_cool_mode": True,
"target_temp_high": 30,
"target_temp_low": 10,
PRESET_AWAY: {
"target_temp_low": 0,
"target_temp_high": 50,
},
}
},
)
await hass.async_block_till_done()

state = hass.states.get(common.ENTITY)
assert state.attributes["supported_features"] == 402

await common.async_set_preset_mode(hass, PRESET_AWAY)

state = hass.states.get(common.ENTITY)
assert state.attributes[ATTR_PRESET_MODE] == PRESET_AWAY


async def test_default_setup_params(
hass: HomeAssistant, setup_comp_dual # noqa: F811
) -> None:
Expand Down

0 comments on commit f8b4adc

Please sign in to comment.