From 1546dbbf25f5124f47438ee37ea2ffdd0928906b Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 24 May 2021 14:03:44 +0200 Subject: [PATCH] Add restore temperature to modbus climate (#50963) * Add restore temperature to climate. * please mypy. * Review 2. --- homeassistant/components/modbus/climate.py | 6 ++- ...test_modbus_climate.py => test_climate.py} | 42 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) rename tests/components/modbus/{test_modbus_climate.py => test_climate.py} (69%) diff --git a/homeassistant/components/modbus/climate.py b/homeassistant/components/modbus/climate.py index d23146bd2ba..4e6a20b1700 100644 --- a/homeassistant/components/modbus/climate.py +++ b/homeassistant/components/modbus/climate.py @@ -20,6 +20,7 @@ from homeassistant.const import ( TEMP_FAHRENHEIT, ) from homeassistant.core import HomeAssistant +from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from .base_platform import BasePlatform @@ -98,7 +99,7 @@ async def async_setup_platform( async_add_entities(entities) -class ModbusThermostat(BasePlatform, ClimateEntity): +class ModbusThermostat(BasePlatform, RestoreEntity, ClimateEntity): """Representation of a Modbus Thermostat.""" def __init__( @@ -131,6 +132,9 @@ class ModbusThermostat(BasePlatform, ClimateEntity): async def async_added_to_hass(self): """Handle entity which will be added.""" await self.async_base_added_to_hass() + state = await self.async_get_last_state() + if state and state.attributes.get(ATTR_TEMPERATURE): + self._target_temperature = float(state.attributes[ATTR_TEMPERATURE]) @property def supported_features(self): diff --git a/tests/components/modbus/test_modbus_climate.py b/tests/components/modbus/test_climate.py similarity index 69% rename from tests/components/modbus/test_modbus_climate.py rename to tests/components/modbus/test_climate.py index 1e14b255ba5..c73a73e47e8 100644 --- a/tests/components/modbus/test_modbus_climate.py +++ b/tests/components/modbus/test_climate.py @@ -2,16 +2,25 @@ import pytest from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN +from homeassistant.components.climate.const import HVAC_MODE_AUTO from homeassistant.components.modbus.const import ( CONF_CLIMATES, CONF_CURRENT_TEMP, CONF_DATA_COUNT, CONF_TARGET_TEMP, ) -from homeassistant.const import CONF_NAME, CONF_SCAN_INTERVAL, CONF_SLAVE +from homeassistant.const import ( + ATTR_TEMPERATURE, + CONF_NAME, + CONF_SCAN_INTERVAL, + CONF_SLAVE, +) +from homeassistant.core import State from .conftest import ReadResult, base_config_test, base_test, prepare_service_update +from tests.common import mock_restore_cache + @pytest.mark.parametrize( "do_options", @@ -101,3 +110,34 @@ async def test_service_climate_update(hass, mock_pymodbus): "homeassistant", "update_entity", {"entity_id": entity_id}, blocking=True ) assert hass.states.get(entity_id).state == "auto" + + +async def test_restore_state_climate(hass): + """Run test for sensor restore state.""" + + climate_name = "test_climate" + test_temp = 37 + entity_id = f"{CLIMATE_DOMAIN}.{climate_name}" + test_value = State(entity_id, 35) + test_value.attributes = {ATTR_TEMPERATURE: test_temp} + config_sensor = { + CONF_NAME: climate_name, + CONF_TARGET_TEMP: 117, + CONF_CURRENT_TEMP: 117, + } + mock_restore_cache( + hass, + (test_value,), + ) + await base_config_test( + hass, + config_sensor, + climate_name, + CLIMATE_DOMAIN, + CONF_CLIMATES, + None, + method_discovery=True, + ) + state = hass.states.get(entity_id) + assert state.state == HVAC_MODE_AUTO + assert state.attributes[ATTR_TEMPERATURE] == test_temp