Add restore temperature to modbus climate (#50963)

* Add restore temperature to climate.

* please mypy.

* Review 2.
pull/51031/head
jan iversen 2021-05-24 14:03:44 +02:00 committed by GitHub
parent 60e65a4bc2
commit 1546dbbf25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 2 deletions

View File

@ -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):

View File

@ -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