From 111a38589e56fdd1caf09a020a2ee435da19e946 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Wed, 7 Dec 2022 00:00:45 +0200 Subject: [PATCH] Shelly code quality - use properties for status (#83421) --- homeassistant/components/shelly/entity.py | 19 +++++++------- homeassistant/components/shelly/light.py | 32 ++++++++++------------- homeassistant/components/shelly/switch.py | 2 +- homeassistant/components/shelly/update.py | 4 +-- homeassistant/components/shelly/utils.py | 10 +++++++ 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 3c57bee8f02..ad4c72a64a0 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -519,20 +519,21 @@ class ShellyRpcAttributeEntity(ShellyRpcEntity, entity.Entity): self._attr_name = get_rpc_entity_name(coordinator.device, key, description.name) self._last_value = None + @property + def sub_status(self) -> Any: + """Device status by entity key.""" + return self.status[self.entity_description.sub_key] + @property def attribute_value(self) -> StateType: """Value of sensor.""" if callable(self.entity_description.value): + # using "get" here since subkey might not exist (e.g. "errors" sub_key) self._last_value = self.entity_description.value( - self.coordinator.device.status[self.key].get( - self.entity_description.sub_key - ), - self._last_value, + self.status.get(self.entity_description.sub_key), self._last_value ) else: - self._last_value = self.coordinator.device.status[self.key][ - self.entity_description.sub_key - ] + self._last_value = self.sub_status return self._last_value @@ -544,9 +545,7 @@ class ShellyRpcAttributeEntity(ShellyRpcEntity, entity.Entity): if not available or not self.entity_description.available: return available - return self.entity_description.available( - self.coordinator.device.status[self.key][self.entity_description.sub_key] - ) + return self.entity_description.available(self.sub_status) class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity, RestoreEntity): diff --git a/homeassistant/components/shelly/light.py b/homeassistant/components/shelly/light.py index fc44774db02..18c1da8a795 100644 --- a/homeassistant/components/shelly/light.py +++ b/homeassistant/components/shelly/light.py @@ -39,10 +39,12 @@ from .coordinator import ShellyBlockCoordinator, ShellyRpcCoordinator, get_entry from .entity import ShellyBlockEntity, ShellyRpcEntity from .utils import ( async_remove_shelly_entity, + brightness_to_percentage, get_device_entry_gen, get_rpc_key_ids, is_block_channel_type_light, is_rpc_channel_type_light, + percentage_to_brightness, ) @@ -189,19 +191,15 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity): @property def brightness(self) -> int: """Return the brightness of this light between 0..255.""" - brightness_pct: int if self.mode == "color": if self.control_result: - brightness_pct = self.control_result["gain"] - else: - brightness_pct = cast(int, self.block.gain) - else: - if self.control_result: - brightness_pct = self.control_result["brightness"] - else: - brightness_pct = cast(int, self.block.brightness) + return percentage_to_brightness(self.control_result["gain"]) + return percentage_to_brightness(cast(int, self.block.gain)) - return round(255 * brightness_pct / 100) + # white mode + if self.control_result: + return percentage_to_brightness(self.control_result["brightness"]) + return percentage_to_brightness(cast(int, self.block.brightness)) @property def color_mode(self) -> ColorMode: @@ -292,11 +290,10 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity): ) if ATTR_BRIGHTNESS in kwargs and brightness_supported(supported_color_modes): - brightness_pct = int(100 * (kwargs[ATTR_BRIGHTNESS] + 1) / 255) if hasattr(self.block, "gain"): - params["gain"] = brightness_pct + params["gain"] = brightness_to_percentage(kwargs[ATTR_BRIGHTNESS]) if hasattr(self.block, "brightness"): - params["brightness"] = brightness_pct + params["brightness"] = brightness_to_percentage(kwargs[ATTR_BRIGHTNESS]) if ( ATTR_COLOR_TEMP_KELVIN in kwargs @@ -386,7 +383,7 @@ class RpcShellySwitchAsLight(ShellyRpcEntity, LightEntity): @property def is_on(self) -> bool: """If light is on.""" - return bool(self.coordinator.device.status[self.key]["output"]) + return bool(self.status["output"]) async def async_turn_on(self, **kwargs: Any) -> None: """Turn on light.""" @@ -411,20 +408,19 @@ class RpcShellyLight(ShellyRpcEntity, LightEntity): @property def is_on(self) -> bool: """If light is on.""" - return bool(self.coordinator.device.status[self.key]["output"]) + return bool(self.status["output"]) @property def brightness(self) -> int: """Return the brightness of this light between 0..255.""" - brightness_pct = self.coordinator.device.status[self.key]["brightness"] - return round(255 * brightness_pct / 100) + return percentage_to_brightness(self.status["brightness"]) async def async_turn_on(self, **kwargs: Any) -> None: """Turn on light.""" params: dict[str, Any] = {"id": self._id, "on": True} if ATTR_BRIGHTNESS in kwargs: - params["brightness"] = int(100 * (kwargs[ATTR_BRIGHTNESS] + 1) / 255) + params["brightness"] = brightness_to_percentage(kwargs[ATTR_BRIGHTNESS]) await self.call_rpc("Light.Set", params) diff --git a/homeassistant/components/shelly/switch.py b/homeassistant/components/shelly/switch.py index 39e754eaf86..3f5186a2017 100644 --- a/homeassistant/components/shelly/switch.py +++ b/homeassistant/components/shelly/switch.py @@ -138,7 +138,7 @@ class RpcRelaySwitch(ShellyRpcEntity, SwitchEntity): @property def is_on(self) -> bool: """If switch is on.""" - return bool(self.coordinator.device.status[self.key]["output"]) + return bool(self.status["output"]) async def async_turn_on(self, **kwargs: Any) -> None: """Turn on relay.""" diff --git a/homeassistant/components/shelly/update.py b/homeassistant/components/shelly/update.py index d801d0d03b3..58bd3b4b8b1 100644 --- a/homeassistant/components/shelly/update.py +++ b/homeassistant/components/shelly/update.py @@ -251,9 +251,7 @@ class RpcUpdateEntity(ShellyRpcAttributeEntity, UpdateEntity): @property def latest_version(self) -> str | None: """Latest version available for install.""" - new_version = self.entity_description.latest_version( - self.coordinator.device.status[self.key][self.entity_description.sub_key], - ) + new_version = self.entity_description.latest_version(self.sub_status) if new_version: return cast(str, new_version) diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index a8663b56287..418cec64d40 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -398,3 +398,13 @@ def device_update_info( dev_registry.async_update_device( device.id, sw_version=shellydevice.firmware_version ) + + +def brightness_to_percentage(brightness: int) -> int: + """Convert brightness level to percentage.""" + return int(100 * (brightness + 1) / 255) + + +def percentage_to_brightness(percentage: int) -> int: + """Convert percentage to brightness level.""" + return round(255 * percentage / 100)