Shelly code quality - use properties for status (#83421)

This commit is contained in:
Shay Levy 2022-12-07 00:00:45 +02:00 committed by GitHub
parent 1a9302b8f6
commit 111a38589e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 32 deletions

View file

@ -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):

View file

@ -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)

View file

@ -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."""

View file

@ -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)

View file

@ -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)