From 7026ea643e63123c3e9c81c45a48c35d8d9699ac Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Sun, 2 Jul 2023 18:51:11 +0300 Subject: [PATCH] Add action attribute to generic hygrostat (#95675) * add action attribute to generic hygrostat * Simplified initialization --- .../components/generic_hygrostat/humidifier.py | 11 +++++++++++ tests/components/generic_hygrostat/test_humidifier.py | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/homeassistant/components/generic_hygrostat/humidifier.py b/homeassistant/components/generic_hygrostat/humidifier.py index 01945f9e242..959b0a8e8df 100644 --- a/homeassistant/components/generic_hygrostat/humidifier.py +++ b/homeassistant/components/generic_hygrostat/humidifier.py @@ -9,6 +9,7 @@ from homeassistant.components.humidifier import ( MODE_AWAY, MODE_NORMAL, PLATFORM_SCHEMA, + HumidifierAction, HumidifierDeviceClass, HumidifierEntity, HumidifierEntityFeature, @@ -158,6 +159,7 @@ class GenericHygrostat(HumidifierEntity, RestoreEntity): 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.""" @@ -361,6 +363,15 @@ class GenericHygrostat(HumidifierEntity, RestoreEntity): """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 341571fe9ad..dcb1608b710 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