From 718da64728260cbe0e59f424838d31d22470b278 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Thu, 3 Feb 2022 23:34:01 +0100 Subject: [PATCH] Improve code quality sensibo (#65503) --- homeassistant/components/sensibo/climate.py | 63 +++++++-------------- 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/homeassistant/components/sensibo/climate.py b/homeassistant/components/sensibo/climate.py index 16d7f8601c9..b0cee4fbac0 100644 --- a/homeassistant/components/sensibo/climate.py +++ b/homeassistant/components/sensibo/climate.py @@ -2,7 +2,6 @@ from __future__ import annotations import asyncio -from typing import Any from aiohttp.client_exceptions import ClientConnectionError import async_timeout @@ -71,6 +70,14 @@ SENSIBO_TO_HA = { HA_TO_SENSIBO = {value: key for key, value in SENSIBO_TO_HA.items()} +AC_STATE_TO_DATA = { + "targetTemperature": "target_temp", + "fanLevel": "fan_mode", + "on": "on", + "mode": "hvac_mode", + "swing": "swing_mode", +} + async def async_setup_platform( hass: HomeAssistant, @@ -253,68 +260,39 @@ class SensiboClimate(CoordinatorEntity, ClimateEntity): else: return - result = await self._async_set_ac_state_property( - "targetTemperature", int(temperature) - ) - if result: - self.coordinator.data[self.unique_id]["target_temp"] = int(temperature) - self.async_write_ha_state() + await self._async_set_ac_state_property("targetTemperature", int(temperature)) async def async_set_fan_mode(self, fan_mode: str) -> None: """Set new target fan mode.""" - result = await self._async_set_ac_state_property("fanLevel", fan_mode) - if result: - self.coordinator.data[self.unique_id]["fan_mode"] = fan_mode - self.async_write_ha_state() + await self._async_set_ac_state_property("fanLevel", fan_mode) async def async_set_hvac_mode(self, hvac_mode: str) -> None: """Set new target operation mode.""" if hvac_mode == HVAC_MODE_OFF: - result = await self._async_set_ac_state_property("on", False) - if result: - self.coordinator.data[self.unique_id]["on"] = False - self.async_write_ha_state() + await self._async_set_ac_state_property("on", False) return # Turn on if not currently on. if not self.coordinator.data[self.unique_id]["on"]: - result = await self._async_set_ac_state_property("on", True) - if result: - self.coordinator.data[self.unique_id]["on"] = True + await self._async_set_ac_state_property("on", True) - result = await self._async_set_ac_state_property( - "mode", HA_TO_SENSIBO[hvac_mode] - ) - if result: - self.coordinator.data[self.unique_id]["hvac_mode"] = HA_TO_SENSIBO[ - hvac_mode - ] - self.async_write_ha_state() + await self._async_set_ac_state_property("mode", HA_TO_SENSIBO[hvac_mode]) async def async_set_swing_mode(self, swing_mode: str) -> None: """Set new target swing operation.""" - result = await self._async_set_ac_state_property("swing", swing_mode) - if result: - self.coordinator.data[self.unique_id]["swing_mode"] = swing_mode - self.async_write_ha_state() + await self._async_set_ac_state_property("swing", swing_mode) async def async_turn_on(self) -> None: """Turn Sensibo unit on.""" - result = await self._async_set_ac_state_property("on", True) - if result: - self.coordinator.data[self.unique_id]["on"] = True - self.async_write_ha_state() + await self._async_set_ac_state_property("on", True) async def async_turn_off(self) -> None: """Turn Sensibo unit on.""" - result = await self._async_set_ac_state_property("on", False) - if result: - self.coordinator.data[self.unique_id]["on"] = False - self.async_write_ha_state() + await self._async_set_ac_state_property("on", False) async def _async_set_ac_state_property( - self, name: str, value: Any, assumed_state: bool = False - ) -> bool: + self, name: str, value: str | int | bool, assumed_state: bool = False + ) -> None: """Set AC state.""" result = {} try: @@ -336,7 +314,10 @@ class SensiboClimate(CoordinatorEntity, ClimateEntity): ) from err LOGGER.debug("Result: %s", result) if result["status"] == "Success": - return True + self.coordinator.data[self.unique_id][AC_STATE_TO_DATA[name]] = value + self.async_write_ha_state() + return + failure = result["failureReason"] raise HomeAssistantError( f"Could not set state for device {self.name} due to reason {failure}"