diff --git a/homeassistant/components/mqtt/climate.py b/homeassistant/components/mqtt/climate.py index c8a50b523e8..99b51d2ab3e 100644 --- a/homeassistant/components/mqtt/climate.py +++ b/homeassistant/components/mqtt/climate.py @@ -439,6 +439,13 @@ class MqttClimate(MqttEntity, ClimateEntity): discovery_data: DiscoveryInfoType | None, ) -> None: """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) @staticmethod @@ -463,29 +470,23 @@ class MqttClimate(MqttEntity, ClimateEntity): 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] - 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] - 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] - 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] - 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 - 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 - 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 + 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 if self._feature_preset_mode: presets = [] @@ -499,9 +500,6 @@ class MqttClimate(MqttEntity, ClimateEntity): self._optimistic_preset_mode = ( 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] = {} for key in VALUE_TEMPLATE_KEYS: diff --git a/tests/components/mqtt/test_climate.py b/tests/components/mqtt/test_climate.py index 67c6e2b4a82..817d0734567 100644 --- a/tests/components/mqtt/test_climate.py +++ b/tests/components/mqtt/test_climate.py @@ -258,7 +258,7 @@ async def test_set_operation_optimistic(hass, mqtt_mock_entry_with_yaml_config): await mqtt_mock_entry_with_yaml_config() state = hass.states.get(ENTITY_CLIMATE) - assert state.state == "unknown" + assert state.state == "off" await common.async_set_hvac_mode(hass, "cool", 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() 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) 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() 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) state = hass.states.get(ENTITY_CLIMATE) @@ -550,7 +550,7 @@ async def test_set_target_temperature_optimistic( await mqtt_mock_entry_with_yaml_config() 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_temperature(hass, temperature=17, entity_id=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() state = hass.states.get(ENTITY_CLIMATE) - assert state.attributes.get("target_temp_low") is None - assert state.attributes.get("target_temp_high") is None + assert state.attributes.get("target_temp_low") == 21 + assert state.attributes.get("target_temp_high") == 21 await common.async_set_temperature( hass, target_temp_low=20, target_temp_high=23, entity_id=ENTITY_CLIMATE )