From f5dfefb3a6213f712b70c388f02ba5a565840a81 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Mon, 17 Jun 2024 22:17:05 +0200 Subject: [PATCH] Use the humidity value in Shelly Wall Display climate entity (#119830) * Use the humidity value with the climate entity if available * Update tests * Use walrus --------- Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com> --- homeassistant/components/shelly/climate.py | 12 ++++++++++ tests/components/shelly/conftest.py | 1 + tests/components/shelly/test_climate.py | 28 ++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/homeassistant/components/shelly/climate.py b/homeassistant/components/shelly/climate.py index a4dc71f870c..ab1e58583d9 100644 --- a/homeassistant/components/shelly/climate.py +++ b/homeassistant/components/shelly/climate.py @@ -468,6 +468,10 @@ class RpcClimate(ShellyRpcEntity, ClimateEntity): self._attr_hvac_modes = [HVACMode.OFF, HVACMode.COOL] else: self._attr_hvac_modes = [HVACMode.OFF, HVACMode.HEAT] + self._humidity_key: str | None = None + # Check if there is a corresponding humidity key for the thermostat ID + if (humidity_key := f"humidity:{id_}") in self.coordinator.device.status: + self._humidity_key = humidity_key @property def target_temperature(self) -> float | None: @@ -479,6 +483,14 @@ class RpcClimate(ShellyRpcEntity, ClimateEntity): """Return current temperature.""" return cast(float, self.status["current_C"]) + @property + def current_humidity(self) -> float | None: + """Return current humidity.""" + if self._humidity_key is None: + return None + + return cast(float, self.coordinator.device.status[self._humidity_key]["rh"]) + @property def hvac_mode(self) -> HVACMode: """HVAC current mode.""" diff --git a/tests/components/shelly/conftest.py b/tests/components/shelly/conftest.py index 6099a16d52e..8e41cbe060f 100644 --- a/tests/components/shelly/conftest.py +++ b/tests/components/shelly/conftest.py @@ -254,6 +254,7 @@ MOCK_STATUS_RPC = { "current_C": 12.3, "output": True, }, + "humidity:0": {"rh": 44.4}, "sys": { "available_updates": { "beta": {"version": "some_beta_version"}, diff --git a/tests/components/shelly/test_climate.py b/tests/components/shelly/test_climate.py index ed4ceea0306..fea46b1d2d1 100644 --- a/tests/components/shelly/test_climate.py +++ b/tests/components/shelly/test_climate.py @@ -8,6 +8,7 @@ from aioshelly.exceptions import DeviceConnectionError, InvalidAuthError import pytest from homeassistant.components.climate import ( + ATTR_CURRENT_HUMIDITY, ATTR_CURRENT_TEMPERATURE, ATTR_HVAC_ACTION, ATTR_HVAC_MODE, @@ -610,6 +611,7 @@ async def test_rpc_climate_hvac_mode( assert state.attributes[ATTR_TEMPERATURE] == 23 assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 12.3 assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.HEATING + assert state.attributes[ATTR_CURRENT_HUMIDITY] == 44.4 entry = entity_registry.async_get(ENTITY_ID) assert entry @@ -620,6 +622,7 @@ async def test_rpc_climate_hvac_mode( state = hass.states.get(ENTITY_ID) assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.IDLE + assert state.attributes[ATTR_CURRENT_HUMIDITY] == 44.4 monkeypatch.setitem(mock_rpc_device.status["thermostat:0"], "enable", False) await hass.services.async_call( @@ -637,6 +640,31 @@ async def test_rpc_climate_hvac_mode( assert state.state == HVACMode.OFF +async def test_rpc_climate_without_humidity( + hass: HomeAssistant, + entity_registry: EntityRegistry, + mock_rpc_device: Mock, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Test climate entity without the humidity value.""" + new_status = deepcopy(mock_rpc_device.status) + new_status.pop("humidity:0") + monkeypatch.setattr(mock_rpc_device, "status", new_status) + + await init_integration(hass, 2, model=MODEL_WALL_DISPLAY) + + state = hass.states.get(ENTITY_ID) + assert state.state == HVACMode.HEAT + assert state.attributes[ATTR_TEMPERATURE] == 23 + assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 12.3 + assert state.attributes[ATTR_HVAC_ACTION] == HVACAction.HEATING + assert ATTR_CURRENT_HUMIDITY not in state.attributes + + entry = entity_registry.async_get(ENTITY_ID) + assert entry + assert entry.unique_id == "123456789ABC-thermostat:0" + + async def test_rpc_climate_set_temperature( hass: HomeAssistant, mock_rpc_device: Mock, monkeypatch: pytest.MonkeyPatch ) -> None: