Update generic_thermostat current_temperature on startup (#43951)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Nigel Rook 2020-12-07 12:14:54 +00:00 committed by GitHub
parent fec623fb4e
commit 1d0b4290fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 3 deletions

View file

@ -33,7 +33,7 @@ from homeassistant.const import (
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import DOMAIN as HA_DOMAIN, callback
from homeassistant.core import DOMAIN as HA_DOMAIN, CoreState, callback
from homeassistant.helpers import condition
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import (
@ -207,7 +207,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity):
)
@callback
def _async_startup(event):
def _async_startup(*_):
"""Init on startup."""
sensor_state = self.hass.states.get(self.sensor_entity_id)
if sensor_state and sensor_state.state not in (
@ -215,8 +215,12 @@ class GenericThermostat(ClimateEntity, RestoreEntity):
STATE_UNKNOWN,
):
self._async_update_temp(sensor_state)
self.async_write_ha_state()
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup)
if self.hass.state == CoreState.running:
_async_startup()
else:
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup)
# Check If we have an old state
old_state = await self.async_get_last_state()

View file

@ -209,6 +209,30 @@ async def test_setup_defaults_to_unknown(hass):
assert HVAC_MODE_OFF == hass.states.get(ENTITY).state
async def test_setup_gets_current_temp_from_sensor(hass):
"""Test that current temperature is updated on entity addition."""
hass.config.units = METRIC_SYSTEM
_setup_sensor(hass, 18)
await hass.async_block_till_done()
await async_setup_component(
hass,
DOMAIN,
{
"climate": {
"platform": "generic_thermostat",
"name": "test",
"cold_tolerance": 2,
"hot_tolerance": 4,
"heater": ENT_SWITCH,
"target_sensor": ENT_SENSOR,
"away_temp": 16,
}
},
)
await hass.async_block_till_done()
assert hass.states.get(ENTITY).attributes["current_temperature"] == 18
async def test_default_setup_params(hass, setup_comp_2):
"""Test the setup with default parameters."""
state = hass.states.get(ENTITY)