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._attr_name = get_rpc_entity_name(coordinator.device, key, description.name)
self._last_value = None self._last_value = None
@property
def sub_status(self) -> Any:
"""Device status by entity key."""
return self.status[self.entity_description.sub_key]
@property @property
def attribute_value(self) -> StateType: def attribute_value(self) -> StateType:
"""Value of sensor.""" """Value of sensor."""
if callable(self.entity_description.value): 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._last_value = self.entity_description.value(
self.coordinator.device.status[self.key].get( self.status.get(self.entity_description.sub_key), self._last_value
self.entity_description.sub_key
),
self._last_value,
) )
else: else:
self._last_value = self.coordinator.device.status[self.key][ self._last_value = self.sub_status
self.entity_description.sub_key
]
return self._last_value return self._last_value
@ -544,9 +545,7 @@ class ShellyRpcAttributeEntity(ShellyRpcEntity, entity.Entity):
if not available or not self.entity_description.available: if not available or not self.entity_description.available:
return available return available
return self.entity_description.available( return self.entity_description.available(self.sub_status)
self.coordinator.device.status[self.key][self.entity_description.sub_key]
)
class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity, RestoreEntity): class ShellySleepingBlockAttributeEntity(ShellyBlockAttributeEntity, RestoreEntity):

View file

@ -39,10 +39,12 @@ from .coordinator import ShellyBlockCoordinator, ShellyRpcCoordinator, get_entry
from .entity import ShellyBlockEntity, ShellyRpcEntity from .entity import ShellyBlockEntity, ShellyRpcEntity
from .utils import ( from .utils import (
async_remove_shelly_entity, async_remove_shelly_entity,
brightness_to_percentage,
get_device_entry_gen, get_device_entry_gen,
get_rpc_key_ids, get_rpc_key_ids,
is_block_channel_type_light, is_block_channel_type_light,
is_rpc_channel_type_light, is_rpc_channel_type_light,
percentage_to_brightness,
) )
@ -189,19 +191,15 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity):
@property @property
def brightness(self) -> int: def brightness(self) -> int:
"""Return the brightness of this light between 0..255.""" """Return the brightness of this light between 0..255."""
brightness_pct: int
if self.mode == "color": if self.mode == "color":
if self.control_result: if self.control_result:
brightness_pct = self.control_result["gain"] return percentage_to_brightness(self.control_result["gain"])
else: return percentage_to_brightness(cast(int, self.block.gain))
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 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 @property
def color_mode(self) -> ColorMode: def color_mode(self) -> ColorMode:
@ -292,11 +290,10 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity):
) )
if ATTR_BRIGHTNESS in kwargs and brightness_supported(supported_color_modes): 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"): if hasattr(self.block, "gain"):
params["gain"] = brightness_pct params["gain"] = brightness_to_percentage(kwargs[ATTR_BRIGHTNESS])
if hasattr(self.block, "brightness"): if hasattr(self.block, "brightness"):
params["brightness"] = brightness_pct params["brightness"] = brightness_to_percentage(kwargs[ATTR_BRIGHTNESS])
if ( if (
ATTR_COLOR_TEMP_KELVIN in kwargs ATTR_COLOR_TEMP_KELVIN in kwargs
@ -386,7 +383,7 @@ class RpcShellySwitchAsLight(ShellyRpcEntity, LightEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""If light is on.""" """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: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on light.""" """Turn on light."""
@ -411,20 +408,19 @@ class RpcShellyLight(ShellyRpcEntity, LightEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""If light is on.""" """If light is on."""
return bool(self.coordinator.device.status[self.key]["output"]) return bool(self.status["output"])
@property @property
def brightness(self) -> int: def brightness(self) -> int:
"""Return the brightness of this light between 0..255.""" """Return the brightness of this light between 0..255."""
brightness_pct = self.coordinator.device.status[self.key]["brightness"] return percentage_to_brightness(self.status["brightness"])
return round(255 * brightness_pct / 100)
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on light.""" """Turn on light."""
params: dict[str, Any] = {"id": self._id, "on": True} params: dict[str, Any] = {"id": self._id, "on": True}
if ATTR_BRIGHTNESS in kwargs: 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) await self.call_rpc("Light.Set", params)

View file

@ -138,7 +138,7 @@ class RpcRelaySwitch(ShellyRpcEntity, SwitchEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""If switch is on.""" """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: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on relay.""" """Turn on relay."""

View file

@ -251,9 +251,7 @@ class RpcUpdateEntity(ShellyRpcAttributeEntity, UpdateEntity):
@property @property
def latest_version(self) -> str | None: def latest_version(self) -> str | None:
"""Latest version available for install.""" """Latest version available for install."""
new_version = self.entity_description.latest_version( new_version = self.entity_description.latest_version(self.sub_status)
self.coordinator.device.status[self.key][self.entity_description.sub_key],
)
if new_version: if new_version:
return cast(str, new_version) return cast(str, new_version)

View file

@ -398,3 +398,13 @@ def device_update_info(
dev_registry.async_update_device( dev_registry.async_update_device(
device.id, sw_version=shellydevice.firmware_version 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)