Correct initial config of MQTT climate (#85097)

* Do not reset MQTT climate state on re-config

* More corrections

* Correct startup behavior in optimistic mode
This commit is contained in:
Jan Bouwhuis 2023-01-04 10:29:02 +01:00 committed by GitHub
parent 49b1d6e7fe
commit b29c96639b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 23 deletions

View File

@ -439,6 +439,13 @@ class MqttClimate(MqttEntity, ClimateEntity):
discovery_data: DiscoveryInfoType | None, discovery_data: DiscoveryInfoType | None,
) -> None: ) -> None:
"""Initialize the climate device.""" """Initialize the climate device."""
self._attr_fan_mode = None
self._attr_hvac_action = None
self._attr_hvac_mode = None
self._attr_is_aux_heat = None
self._attr_swing_mode = None
self._attr_target_temperature_low = None
self._attr_target_temperature_high = None
MqttEntity.__init__(self, hass, config, config_entry, discovery_data) MqttEntity.__init__(self, hass, config, config_entry, discovery_data)
@staticmethod @staticmethod
@ -463,29 +470,23 @@ class MqttClimate(MqttEntity, ClimateEntity):
self._topic = {key: config.get(key) for key in TOPIC_KEYS} self._topic = {key: config.get(key) for key in TOPIC_KEYS}
# set to None in non-optimistic mode
self._attr_target_temperature = None
self._attr_fan_mode = None
self._attr_hvac_mode = None
self._attr_swing_mode = None
self._attr_target_temperature_low = None
self._attr_target_temperature_high = None
self._optimistic = config[CONF_OPTIMISTIC] self._optimistic = config[CONF_OPTIMISTIC]
if self._topic[CONF_TEMP_STATE_TOPIC] is None: if self._topic[CONF_TEMP_STATE_TOPIC] is None or self._optimistic:
self._attr_target_temperature = config[CONF_TEMP_INITIAL] self._attr_target_temperature = config[CONF_TEMP_INITIAL]
if self._topic[CONF_TEMP_LOW_STATE_TOPIC] is None: if self._topic[CONF_TEMP_LOW_STATE_TOPIC] is None or self._optimistic:
self._attr_target_temperature_low = config[CONF_TEMP_INITIAL] self._attr_target_temperature_low = config[CONF_TEMP_INITIAL]
if self._topic[CONF_TEMP_HIGH_STATE_TOPIC] is None: if self._topic[CONF_TEMP_HIGH_STATE_TOPIC] is None or self._optimistic:
self._attr_target_temperature_high = config[CONF_TEMP_INITIAL] self._attr_target_temperature_high = config[CONF_TEMP_INITIAL]
if self._topic[CONF_FAN_MODE_STATE_TOPIC] is None: if self._topic[CONF_FAN_MODE_STATE_TOPIC] is None or self._optimistic:
self._attr_fan_mode = FAN_LOW self._attr_fan_mode = FAN_LOW
if self._topic[CONF_SWING_MODE_STATE_TOPIC] is None: if self._topic[CONF_SWING_MODE_STATE_TOPIC] is None or self._optimistic:
self._attr_swing_mode = SWING_OFF self._attr_swing_mode = SWING_OFF
if self._topic[CONF_MODE_STATE_TOPIC] is None: if self._topic[CONF_MODE_STATE_TOPIC] is None or self._optimistic:
self._attr_hvac_mode = HVACMode.OFF self._attr_hvac_mode = HVACMode.OFF
if self._topic[CONF_AUX_STATE_TOPIC] is None or self._optimistic:
self._attr_is_aux_heat = False
self._feature_preset_mode = CONF_PRESET_MODE_COMMAND_TOPIC in config self._feature_preset_mode = CONF_PRESET_MODE_COMMAND_TOPIC in config
if self._feature_preset_mode: if self._feature_preset_mode:
presets = [] presets = []
@ -499,9 +500,6 @@ class MqttClimate(MqttEntity, ClimateEntity):
self._optimistic_preset_mode = ( self._optimistic_preset_mode = (
self._optimistic or CONF_PRESET_MODE_STATE_TOPIC not in config self._optimistic or CONF_PRESET_MODE_STATE_TOPIC not in config
) )
self._attr_hvac_action = None
self._attr_is_aux_heat = False
value_templates: dict[str, Template | None] = {} value_templates: dict[str, Template | None] = {}
for key in VALUE_TEMPLATE_KEYS: for key in VALUE_TEMPLATE_KEYS:

View File

@ -258,7 +258,7 @@ async def test_set_operation_optimistic(hass, mqtt_mock_entry_with_yaml_config):
await mqtt_mock_entry_with_yaml_config() await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(ENTITY_CLIMATE) state = hass.states.get(ENTITY_CLIMATE)
assert state.state == "unknown" assert state.state == "off"
await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE) await common.async_set_hvac_mode(hass, "cool", ENTITY_CLIMATE)
state = hass.states.get(ENTITY_CLIMATE) state = hass.states.get(ENTITY_CLIMATE)
@ -351,7 +351,7 @@ async def test_set_fan_mode_optimistic(hass, mqtt_mock_entry_with_yaml_config):
await mqtt_mock_entry_with_yaml_config() await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(ENTITY_CLIMATE) state = hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get("fan_mode") is None assert state.attributes.get("fan_mode") == "low"
await common.async_set_fan_mode(hass, "high", ENTITY_CLIMATE) await common.async_set_fan_mode(hass, "high", ENTITY_CLIMATE)
state = hass.states.get(ENTITY_CLIMATE) state = hass.states.get(ENTITY_CLIMATE)
@ -431,7 +431,7 @@ async def test_set_swing_optimistic(hass, mqtt_mock_entry_with_yaml_config):
await mqtt_mock_entry_with_yaml_config() await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(ENTITY_CLIMATE) state = hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get("swing_mode") is None assert state.attributes.get("swing_mode") == "off"
await common.async_set_swing_mode(hass, "on", ENTITY_CLIMATE) await common.async_set_swing_mode(hass, "on", ENTITY_CLIMATE)
state = hass.states.get(ENTITY_CLIMATE) state = hass.states.get(ENTITY_CLIMATE)
@ -550,7 +550,7 @@ async def test_set_target_temperature_optimistic(
await mqtt_mock_entry_with_yaml_config() await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(ENTITY_CLIMATE) state = hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get("temperature") is None assert state.attributes.get("temperature") == 21
await common.async_set_hvac_mode(hass, "heat", ENTITY_CLIMATE) await common.async_set_hvac_mode(hass, "heat", ENTITY_CLIMATE)
await common.async_set_temperature(hass, temperature=17, entity_id=ENTITY_CLIMATE) await common.async_set_temperature(hass, temperature=17, entity_id=ENTITY_CLIMATE)
state = hass.states.get(ENTITY_CLIMATE) state = hass.states.get(ENTITY_CLIMATE)
@ -634,8 +634,8 @@ async def test_set_target_temperature_low_high_optimistic(
await mqtt_mock_entry_with_yaml_config() await mqtt_mock_entry_with_yaml_config()
state = hass.states.get(ENTITY_CLIMATE) state = hass.states.get(ENTITY_CLIMATE)
assert state.attributes.get("target_temp_low") is None assert state.attributes.get("target_temp_low") == 21
assert state.attributes.get("target_temp_high") is None assert state.attributes.get("target_temp_high") == 21
await common.async_set_temperature( await common.async_set_temperature(
hass, target_temp_low=20, target_temp_high=23, entity_id=ENTITY_CLIMATE hass, target_temp_low=20, target_temp_high=23, entity_id=ENTITY_CLIMATE
) )