From 167b9cb1a0f1083df0ed07e7f68b5b9cc2cbdc7c Mon Sep 17 00:00:00 2001 From: G Johansson Date: Fri, 9 Sep 2022 17:06:57 +0200 Subject: [PATCH] Additional cleanup for Sensibo (#78144) * Clean sensibo code * Add function to description --- homeassistant/components/sensibo/button.py | 7 +-- homeassistant/components/sensibo/climate.py | 25 +------- homeassistant/components/sensibo/number.py | 8 +-- homeassistant/components/sensibo/select.py | 5 +- homeassistant/components/sensibo/switch.py | 64 ++++++++------------- 5 files changed, 30 insertions(+), 79 deletions(-) diff --git a/homeassistant/components/sensibo/button.py b/homeassistant/components/sensibo/button.py index b91bddaf882..f1b2c53408d 100644 --- a/homeassistant/components/sensibo/button.py +++ b/homeassistant/components/sensibo/button.py @@ -4,8 +4,6 @@ from __future__ import annotations from dataclasses import dataclass from typing import Any -from pysensibo.model import SensiboDevice - from homeassistant.components.button import ButtonEntity, ButtonEntityDescription from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant @@ -77,15 +75,12 @@ class SensiboDeviceButton(SensiboDeviceBaseEntity, ButtonEntity): async def async_press(self) -> None: """Press the button.""" await self.async_send_api_call( - device_data=self.device_data, key=self.entity_description.data_key, value=False, ) @async_handle_api_call - async def async_send_api_call( - self, device_data: SensiboDevice, key: Any, value: Any - ) -> bool: + async def async_send_api_call(self, key: str, value: Any) -> bool: """Make service call to api.""" result = await self._client.async_reset_filter( self._device_id, diff --git a/homeassistant/components/sensibo/climate.py b/homeassistant/components/sensibo/climate.py index 8bd225f2e4d..36047fe0ddd 100644 --- a/homeassistant/components/sensibo/climate.py +++ b/homeassistant/components/sensibo/climate.py @@ -4,7 +4,6 @@ from __future__ import annotations from bisect import bisect_left from typing import TYPE_CHECKING, Any -from pysensibo.model import SensiboDevice import voluptuous as vol from homeassistant.components.climate import ( @@ -252,7 +251,6 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): new_temp = _find_valid_target_temp(temperature, self.device_data.temp_list) await self.async_send_api_call( - device_data=self.device_data, key=AC_STATE_TO_DATA["targetTemperature"], value=new_temp, name="targetTemperature", @@ -265,7 +263,6 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): raise HomeAssistantError("Current mode doesn't support setting Fanlevel") await self.async_send_api_call( - device_data=self.device_data, key=AC_STATE_TO_DATA["fanLevel"], value=fan_mode, name="fanLevel", @@ -276,7 +273,6 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): """Set new target operation mode.""" if hvac_mode == HVACMode.OFF: await self.async_send_api_call( - device_data=self.device_data, key=AC_STATE_TO_DATA["on"], value=False, name="on", @@ -287,7 +283,6 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): # Turn on if not currently on. if not self.device_data.device_on: await self.async_send_api_call( - device_data=self.device_data, key=AC_STATE_TO_DATA["on"], value=True, name="on", @@ -295,7 +290,6 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): ) await self.async_send_api_call( - device_data=self.device_data, key=AC_STATE_TO_DATA["mode"], value=HA_TO_SENSIBO[hvac_mode], name="mode", @@ -308,7 +302,6 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): raise HomeAssistantError("Current mode doesn't support setting Swing") await self.async_send_api_call( - device_data=self.device_data, key=AC_STATE_TO_DATA["swing"], value=swing_mode, name="swing", @@ -318,7 +311,6 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): async def async_turn_on(self) -> None: """Turn Sensibo unit on.""" await self.async_send_api_call( - device_data=self.device_data, key=AC_STATE_TO_DATA["on"], value=True, name="on", @@ -328,7 +320,6 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): async def async_turn_off(self) -> None: """Turn Sensibo unit on.""" await self.async_send_api_call( - device_data=self.device_data, key=AC_STATE_TO_DATA["on"], value=False, name="on", @@ -338,7 +329,6 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): async def async_assume_state(self, state: str) -> None: """Sync state with api.""" await self.async_send_api_call( - device_data=self.device_data, key=AC_STATE_TO_DATA["on"], value=state != HVACMode.OFF, name="on", @@ -353,10 +343,8 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): "acState": {**self.device_data.ac_states, "on": new_state}, } await self.api_call_custom_service_timer( - device_data=self.device_data, key="timer_on", value=True, - command="set_timer", data=params, ) @@ -385,18 +373,15 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): params["primeIntegration"] = outdoor_integration await self.api_call_custom_service_pure_boost( - device_data=self.device_data, key="pure_boost_enabled", value=True, - command="set_pure_boost", data=params, ) @async_handle_api_call async def async_send_api_call( self, - device_data: SensiboDevice, - key: Any, + key: str, value: Any, name: str, assumed_state: bool = False, @@ -414,10 +399,8 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): @async_handle_api_call async def api_call_custom_service_timer( self, - device_data: SensiboDevice, - key: Any, + key: str, value: Any, - command: str, data: dict, ) -> bool: """Make service call to api.""" @@ -428,10 +411,8 @@ class SensiboClimate(SensiboDeviceBaseEntity, ClimateEntity): @async_handle_api_call async def api_call_custom_service_pure_boost( self, - device_data: SensiboDevice, - key: Any, + key: str, value: Any, - command: str, data: dict, ) -> bool: """Make service call to api.""" diff --git a/homeassistant/components/sensibo/number.py b/homeassistant/components/sensibo/number.py index 6550d7382d3..40ae8d7601b 100644 --- a/homeassistant/components/sensibo/number.py +++ b/homeassistant/components/sensibo/number.py @@ -100,14 +100,10 @@ class SensiboNumber(SensiboDeviceBaseEntity, NumberEntity): async def async_set_native_value(self, value: float) -> None: """Set value for calibration.""" - await self.async_send_api_call( - device_data=self.device_data, key=self.entity_description.key, value=value - ) + await self.async_send_api_call(key=self.entity_description.key, value=value) @async_handle_api_call - async def async_send_api_call( - self, device_data: SensiboDevice, key: Any, value: Any - ) -> bool: + async def async_send_api_call(self, key: str, value: Any) -> bool: """Make service call to api.""" data = {self.entity_description.remote_key: value} result = await self._client.async_set_calibration( diff --git a/homeassistant/components/sensibo/select.py b/homeassistant/components/sensibo/select.py index ab95377d016..afb961b429a 100644 --- a/homeassistant/components/sensibo/select.py +++ b/homeassistant/components/sensibo/select.py @@ -108,15 +108,12 @@ class SensiboSelect(SensiboDeviceBaseEntity, SelectEntity): ) await self.async_send_api_call( - device_data=self.device_data, key=self.entity_description.data_key, value=option, ) @async_handle_api_call - async def async_send_api_call( - self, device_data: SensiboDevice, key: Any, value: Any - ) -> bool: + async def async_send_api_call(self, key: str, value: Any) -> bool: """Make service call to api.""" data = { "name": self.entity_description.key, diff --git a/homeassistant/components/sensibo/switch.py b/homeassistant/components/sensibo/switch.py index c06bf4d1ac6..cefa8ece084 100644 --- a/homeassistant/components/sensibo/switch.py +++ b/homeassistant/components/sensibo/switch.py @@ -49,8 +49,8 @@ DEVICE_SWITCH_TYPES: tuple[SensiboDeviceSwitchEntityDescription, ...] = ( icon="mdi:timer", value_fn=lambda data: data.timer_on, extra_fn=lambda data: {"id": data.timer_id, "turn_on": data.timer_state_on}, - command_on="set_timer", - command_off="del_timer", + command_on="async_turn_on_timer", + command_off="async_turn_off_timer", data_key="timer_on", ), ) @@ -62,8 +62,8 @@ PURE_SWITCH_TYPES: tuple[SensiboDeviceSwitchEntityDescription, ...] = ( name="Pure Boost", value_fn=lambda data: data.pure_boost_enabled, extra_fn=None, - command_on="set_pure_boost", - command_off="set_pure_boost", + command_on="async_turn_on_off_pure_boost", + command_off="async_turn_on_off_pure_boost", data_key="pure_boost_enabled", ), ) @@ -113,33 +113,21 @@ class SensiboDeviceSwitch(SensiboDeviceBaseEntity, SwitchEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" - if self.entity_description.key == "timer_on_switch": - await self.async_turn_on_timer( - device_data=self.device_data, - key=self.entity_description.data_key, - value=True, - ) - if self.entity_description.key == "pure_boost_switch": - await self.async_turn_on_off_pure_boost( - device_data=self.device_data, - key=self.entity_description.data_key, - value=True, - ) + func = getattr(SensiboDeviceSwitch, self.entity_description.command_on) + await func( + self, + key=self.entity_description.data_key, + value=True, + ) async def async_turn_off(self, **kwargs: Any) -> None: """Turn the entity off.""" - if self.entity_description.key == "timer_on_switch": - await self.async_turn_off_timer( - device_data=self.device_data, - key=self.entity_description.data_key, - value=False, - ) - if self.entity_description.key == "pure_boost_switch": - await self.async_turn_on_off_pure_boost( - device_data=self.device_data, - key=self.entity_description.data_key, - value=False, - ) + func = getattr(SensiboDeviceSwitch, self.entity_description.command_off) + await func( + self, + key=self.entity_description.data_key, + value=True, + ) @property def extra_state_attributes(self) -> Mapping[str, Any] | None: @@ -149,37 +137,31 @@ class SensiboDeviceSwitch(SensiboDeviceBaseEntity, SwitchEntity): return None @async_handle_api_call - async def async_turn_on_timer( - self, device_data: SensiboDevice, key: Any, value: Any - ) -> bool: + async def async_turn_on_timer(self, key: str, value: Any) -> bool: """Make service call to api for setting timer.""" result = {} - new_state = bool(device_data.ac_states["on"] is False) + new_state = bool(self.device_data.ac_states["on"] is False) data = { "minutesFromNow": 60, - "acState": {**device_data.ac_states, "on": new_state}, + "acState": {**self.device_data.ac_states, "on": new_state}, } result = await self._client.async_set_timer(self._device_id, data) return bool(result.get("status") == "success") @async_handle_api_call - async def async_turn_off_timer( - self, device_data: SensiboDevice, key: Any, value: Any - ) -> bool: + async def async_turn_off_timer(self, key: str, value: Any) -> bool: """Make service call to api for deleting timer.""" result = {} result = await self._client.async_del_timer(self._device_id) return bool(result.get("status") == "success") @async_handle_api_call - async def async_turn_on_off_pure_boost( - self, device_data: SensiboDevice, key: Any, value: Any - ) -> bool: + async def async_turn_on_off_pure_boost(self, key: str, value: Any) -> bool: """Make service call to api for setting Pure Boost.""" result = {} - new_state = bool(device_data.pure_boost_enabled is False) + new_state = bool(self.device_data.pure_boost_enabled is False) data: dict[str, Any] = {"enabled": new_state} - if device_data.pure_measure_integration is None: + if self.device_data.pure_measure_integration is None: data["sensitivity"] = "N" data["measurementsIntegration"] = True data["acIntegration"] = False