Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion homeassistant/components/modbus/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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
Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -131,6 +132,9 @@ def __init__(
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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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