From d58167b305fd23a4c74822dd0ea4fcf48d20217b Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Sat, 1 Jul 2023 17:40:53 +0300 Subject: [PATCH 1/2] add action attribute to generic hygrostat --- .../components/generic_hygrostat/humidifier.py | 18 ++++++++++++++++++ .../generic_hygrostat/test_humidifier.py | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/homeassistant/components/generic_hygrostat/humidifier.py b/homeassistant/components/generic_hygrostat/humidifier.py index 01945f9e242daf..b1b84a323dbafb 100644 --- a/homeassistant/components/generic_hygrostat/humidifier.py +++ b/homeassistant/components/generic_hygrostat/humidifier.py @@ -9,6 +9,7 @@ MODE_AWAY, MODE_NORMAL, PLATFORM_SCHEMA, + HumidifierAction, HumidifierDeviceClass, HumidifierEntity, HumidifierEntityFeature, @@ -180,6 +181,14 @@ async def async_added_to_hass(self): async def _async_startup(event): """Init on startup.""" + if self._is_device_active: + if self._device_class == HumidifierDeviceClass.DEHUMIDIFIER: + self._attr_action = HumidifierAction.DRYING + else: + self._attr_action = HumidifierAction.HUMIDIFYING + else: + self._attr_action = HumidifierAction.IDLE + sensor_state = self.hass.states.get(self._sensor_entity_id) if sensor_state is None or sensor_state.state in ( STATE_UNKNOWN, @@ -361,6 +370,15 @@ def _async_switch_changed(self, entity_id, old_state, new_state): """Handle humidifier switch state changes.""" if new_state is None: return + + if new_state.state == STATE_ON: + if self._device_class == HumidifierDeviceClass.DEHUMIDIFIER: + self._attr_action = HumidifierAction.DRYING + else: + self._attr_action = HumidifierAction.HUMIDIFYING + else: + self._attr_action = HumidifierAction.IDLE + self.async_schedule_update_ha_state() async def _async_update_humidity(self, humidity): diff --git a/tests/components/generic_hygrostat/test_humidifier.py b/tests/components/generic_hygrostat/test_humidifier.py index 341571fe9ad4f6..dcb1608b7102ef 100644 --- a/tests/components/generic_hygrostat/test_humidifier.py +++ b/tests/components/generic_hygrostat/test_humidifier.py @@ -121,6 +121,7 @@ async def test_humidifier_input_boolean(hass: HomeAssistant, setup_comp_1) -> No await hass.async_block_till_done() assert hass.states.get(humidifier_switch).state == STATE_ON + assert hass.states.get(ENTITY).attributes.get("action") == "humidifying" async def test_humidifier_switch( @@ -165,6 +166,7 @@ async def test_humidifier_switch( await hass.async_block_till_done() assert hass.states.get(humidifier_switch).state == STATE_ON + assert hass.states.get(ENTITY).attributes.get("action") == "humidifying" def _setup_sensor(hass, humidity): @@ -277,6 +279,7 @@ async def test_default_setup_params(hass: HomeAssistant, setup_comp_2) -> None: assert state.attributes.get("min_humidity") == 0 assert state.attributes.get("max_humidity") == 100 assert state.attributes.get("humidity") == 0 + assert state.attributes.get("action") == "idle" async def test_default_setup_params_dehumidifier( @@ -287,6 +290,7 @@ async def test_default_setup_params_dehumidifier( assert state.attributes.get("min_humidity") == 0 assert state.attributes.get("max_humidity") == 100 assert state.attributes.get("humidity") == 100 + assert state.attributes.get("action") == "idle" async def test_get_modes(hass: HomeAssistant, setup_comp_2) -> None: @@ -648,6 +652,7 @@ async def test_set_target_humidity_dry_off(hass: HomeAssistant, setup_comp_3) -> assert call.domain == HASS_DOMAIN assert call.service == SERVICE_TURN_OFF assert call.data["entity_id"] == ENT_SWITCH + assert hass.states.get(ENTITY).attributes.get("action") == "drying" async def test_turn_away_mode_on_drying(hass: HomeAssistant, setup_comp_3) -> None: @@ -799,6 +804,7 @@ async def test_running_when_operating_mode_is_off_2( assert call.domain == HASS_DOMAIN assert call.service == SERVICE_TURN_OFF assert call.data["entity_id"] == ENT_SWITCH + assert hass.states.get(ENTITY).attributes.get("action") == "off" async def test_no_state_change_when_operation_mode_off_2( @@ -818,6 +824,7 @@ async def test_no_state_change_when_operation_mode_off_2( _setup_sensor(hass, 45) await hass.async_block_till_done() assert len(calls) == 0 + assert hass.states.get(ENTITY).attributes.get("action") == "off" @pytest.fixture From 3c3e7d7870f4341cb46707c8bcd957268212ac72 Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Sat, 1 Jul 2023 17:54:17 +0300 Subject: [PATCH 2/2] Simplified initialization --- homeassistant/components/generic_hygrostat/humidifier.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/homeassistant/components/generic_hygrostat/humidifier.py b/homeassistant/components/generic_hygrostat/humidifier.py index b1b84a323dbafb..959b0a8e8dfcdd 100644 --- a/homeassistant/components/generic_hygrostat/humidifier.py +++ b/homeassistant/components/generic_hygrostat/humidifier.py @@ -159,6 +159,7 @@ def __init__( self._is_away = False if not self._device_class: self._device_class = HumidifierDeviceClass.HUMIDIFIER + self._attr_action = HumidifierAction.IDLE async def async_added_to_hass(self): """Run when entity about to be added.""" @@ -181,14 +182,6 @@ async def async_added_to_hass(self): async def _async_startup(event): """Init on startup.""" - if self._is_device_active: - if self._device_class == HumidifierDeviceClass.DEHUMIDIFIER: - self._attr_action = HumidifierAction.DRYING - else: - self._attr_action = HumidifierAction.HUMIDIFYING - else: - self._attr_action = HumidifierAction.IDLE - sensor_state = self.hass.states.get(self._sensor_entity_id) if sensor_state is None or sensor_state.state in ( STATE_UNKNOWN,