diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py index 4072c43bc27..175ee8f1d5b 100644 --- a/homeassistant/components/generic_thermostat/climate.py +++ b/homeassistant/components/generic_thermostat/climate.py @@ -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() diff --git a/tests/components/generic_thermostat/test_climate.py b/tests/components/generic_thermostat/test_climate.py index eaf7c8e5651..71c6f41282b 100644 --- a/tests/components/generic_thermostat/test_climate.py +++ b/tests/components/generic_thermostat/test_climate.py @@ -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)