Use shorthand attributes in shelly light (#64831)

* Use shorthand attributes in shelly light

* Adjust mypy

* Revert temperature conversions

* Adjust initialisation

* Invert min/max for mired

* Make _attr_supported_color_modes an instance attribute

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2022-01-25 14:48:07 +01:00 committed by GitHub
parent cda2c73f2f
commit da316e1547
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -60,6 +60,10 @@ from .utils import (
_LOGGER: Final = logging.getLogger(__name__)
MIRED_MAX_VALUE_WHITE = color_temperature_kelvin_to_mired(KELVIN_MIN_VALUE_WHITE)
MIRED_MIN_VALUE = color_temperature_kelvin_to_mired(KELVIN_MAX_VALUE)
MIRED_MAX_VALUE_COLOR = color_temperature_kelvin_to_mired(KELVIN_MIN_VALUE_COLOR)
async def async_setup_entry(
hass: HomeAssistant,
@ -130,33 +134,37 @@ async def async_setup_rpc_entry(
class BlockShellyLight(ShellyBlockEntity, LightEntity):
"""Entity that controls a light on block based Shelly devices."""
_attr_supported_color_modes: set[str]
def __init__(self, wrapper: BlockDeviceWrapper, block: Block) -> None:
"""Initialize light."""
super().__init__(wrapper, block)
self.control_result: dict[str, Any] | None = None
self._supported_color_modes: set[str] = set()
self._supported_features: int = 0
self._attr_supported_color_modes = set()
self._attr_min_mireds = MIRED_MIN_VALUE
self._min_kelvin: int = KELVIN_MIN_VALUE_WHITE
self._attr_max_mireds = MIRED_MAX_VALUE_WHITE
self._max_kelvin: int = KELVIN_MAX_VALUE
if hasattr(block, "red") and hasattr(block, "green") and hasattr(block, "blue"):
self._attr_max_mireds = MIRED_MAX_VALUE_COLOR
self._min_kelvin = KELVIN_MIN_VALUE_COLOR
if wrapper.model in RGBW_MODELS:
self._supported_color_modes.add(COLOR_MODE_RGBW)
self._attr_supported_color_modes.add(COLOR_MODE_RGBW)
else:
self._supported_color_modes.add(COLOR_MODE_RGB)
self._attr_supported_color_modes.add(COLOR_MODE_RGB)
if hasattr(block, "colorTemp"):
self._supported_color_modes.add(COLOR_MODE_COLOR_TEMP)
self._attr_supported_color_modes.add(COLOR_MODE_COLOR_TEMP)
if not self._supported_color_modes:
if not self._attr_supported_color_modes:
if hasattr(block, "brightness") or hasattr(block, "gain"):
self._supported_color_modes.add(COLOR_MODE_BRIGHTNESS)
self._attr_supported_color_modes.add(COLOR_MODE_BRIGHTNESS)
else:
self._supported_color_modes.add(COLOR_MODE_ONOFF)
self._attr_supported_color_modes.add(COLOR_MODE_ONOFF)
if hasattr(block, "effect"):
self._supported_features |= SUPPORT_EFFECT
self._attr_supported_features |= SUPPORT_EFFECT
if wrapper.model in MODELS_SUPPORTING_LIGHT_TRANSITION:
match = FIRMWARE_PATTERN.search(wrapper.device.settings.get("fw", ""))
@ -164,12 +172,7 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity):
match is not None
and int(match[0]) >= LIGHT_TRANSITION_MIN_FIRMWARE_DATE
):
self._supported_features |= SUPPORT_TRANSITION
@property
def supported_features(self) -> int:
"""Supported features."""
return self._supported_features
self._attr_supported_features |= SUPPORT_TRANSITION
@property
def is_on(self) -> bool:
@ -264,21 +267,6 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity):
return int(color_temperature_kelvin_to_mired(color_temp))
@property
def min_mireds(self) -> int:
"""Return the coldest color_temp that this light supports."""
return int(color_temperature_kelvin_to_mired(self._max_kelvin))
@property
def max_mireds(self) -> int:
"""Return the warmest color_temp that this light supports."""
return int(color_temperature_kelvin_to_mired(self._min_kelvin))
@property
def supported_color_modes(self) -> set | None:
"""Flag supported color modes."""
return self._supported_color_modes
@property
def effect_list(self) -> list[str] | None:
"""Return the list of supported effects."""
@ -314,7 +302,7 @@ class BlockShellyLight(ShellyBlockEntity, LightEntity):
return
set_mode = None
supported_color_modes = self._supported_color_modes
supported_color_modes = self._attr_supported_color_modes
params: dict[str, Any] = {"turn": "on"}
if ATTR_TRANSITION in kwargs: