Improve code quality sensibo (#65503)

This commit is contained in:
G Johansson 2022-02-03 23:34:01 +01:00 committed by GitHub
parent 157276f4e6
commit 718da64728
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from typing import Any
from aiohttp.client_exceptions import ClientConnectionError from aiohttp.client_exceptions import ClientConnectionError
import async_timeout import async_timeout
@ -71,6 +70,14 @@ SENSIBO_TO_HA = {
HA_TO_SENSIBO = {value: key for key, value in SENSIBO_TO_HA.items()} 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( async def async_setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
@ -253,68 +260,39 @@ class SensiboClimate(CoordinatorEntity, ClimateEntity):
else: else:
return return
result = await self._async_set_ac_state_property( await self._async_set_ac_state_property("targetTemperature", int(temperature))
"targetTemperature", int(temperature)
)
if result:
self.coordinator.data[self.unique_id]["target_temp"] = int(temperature)
self.async_write_ha_state()
async def async_set_fan_mode(self, fan_mode: str) -> None: async def async_set_fan_mode(self, fan_mode: str) -> None:
"""Set new target fan mode.""" """Set new target fan mode."""
result = await self._async_set_ac_state_property("fanLevel", fan_mode) 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()
async def async_set_hvac_mode(self, hvac_mode: str) -> None: async def async_set_hvac_mode(self, hvac_mode: str) -> None:
"""Set new target operation mode.""" """Set new target operation mode."""
if hvac_mode == HVAC_MODE_OFF: if hvac_mode == HVAC_MODE_OFF:
result = await self._async_set_ac_state_property("on", False) await self._async_set_ac_state_property("on", False)
if result:
self.coordinator.data[self.unique_id]["on"] = False
self.async_write_ha_state()
return return
# Turn on if not currently on. # Turn on if not currently on.
if not self.coordinator.data[self.unique_id]["on"]: if not self.coordinator.data[self.unique_id]["on"]:
result = await self._async_set_ac_state_property("on", True) await self._async_set_ac_state_property("on", True)
if result:
self.coordinator.data[self.unique_id]["on"] = True
result = await self._async_set_ac_state_property( await self._async_set_ac_state_property("mode", HA_TO_SENSIBO[hvac_mode])
"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()
async def async_set_swing_mode(self, swing_mode: str) -> None: async def async_set_swing_mode(self, swing_mode: str) -> None:
"""Set new target swing operation.""" """Set new target swing operation."""
result = await self._async_set_ac_state_property("swing", swing_mode) 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()
async def async_turn_on(self) -> None: async def async_turn_on(self) -> None:
"""Turn Sensibo unit on.""" """Turn Sensibo unit on."""
result = await self._async_set_ac_state_property("on", True) await self._async_set_ac_state_property("on", True)
if result:
self.coordinator.data[self.unique_id]["on"] = True
self.async_write_ha_state()
async def async_turn_off(self) -> None: async def async_turn_off(self) -> None:
"""Turn Sensibo unit on.""" """Turn Sensibo unit on."""
result = await self._async_set_ac_state_property("on", False) await self._async_set_ac_state_property("on", False)
if result:
self.coordinator.data[self.unique_id]["on"] = False
self.async_write_ha_state()
async def _async_set_ac_state_property( async def _async_set_ac_state_property(
self, name: str, value: Any, assumed_state: bool = False self, name: str, value: str | int | bool, assumed_state: bool = False
) -> bool: ) -> None:
"""Set AC state.""" """Set AC state."""
result = {} result = {}
try: try:
@ -336,7 +314,10 @@ class SensiboClimate(CoordinatorEntity, ClimateEntity):
) from err ) from err
LOGGER.debug("Result: %s", result) LOGGER.debug("Result: %s", result)
if result["status"] == "Success": 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"] failure = result["failureReason"]
raise HomeAssistantError( raise HomeAssistantError(
f"Could not set state for device {self.name} due to reason {failure}" f"Could not set state for device {self.name} due to reason {failure}"