mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Fix issue with turning the ambilight on after switched off (#69132)
* Refactor ambilight component. Fix issue with turning the ambilight on after switched off * Move dataclass to original location. Add factory method * Remove follow video effect list * Fix log * Remove follow video effect list * Update homeassistant/components/philips_js/light.py Co-authored-by: Joakim Plate <elupus@ecce.se> * Update homeassistant/components/philips_js/light.py Co-authored-by: Joakim Plate <elupus@ecce.se> * Add missing typing * Fix issues with restoring last state Co-authored-by: Joakim Plate <elupus@ecce.se>
This commit is contained in:
parent
e70c8fa359
commit
1da7927fbb
@ -1,6 +1,8 @@
|
|||||||
"""Component to integrate ambilight for TVs exposing the Joint Space API."""
|
"""Component to integrate ambilight for TVs exposing the Joint Space API."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from haphilipsjs import PhilipsTV
|
from haphilipsjs import PhilipsTV
|
||||||
from haphilipsjs.typing import AmbilightCurrentConfiguration
|
from haphilipsjs.typing import AmbilightCurrentConfiguration
|
||||||
|
|
||||||
@ -51,44 +53,54 @@ def _get_settings(style: AmbilightCurrentConfiguration):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _parse_effect(effect: str):
|
@dataclass
|
||||||
style, _, algorithm = effect.partition(EFFECT_PARTITION)
|
class AmbilightEffect:
|
||||||
if style == EFFECT_MODE:
|
"""Data class describing the ambilight effect."""
|
||||||
return EFFECT_MODE, algorithm, None
|
|
||||||
algorithm, _, expert = algorithm.partition(EFFECT_PARTITION)
|
|
||||||
if expert:
|
|
||||||
return EFFECT_EXPERT, style, algorithm
|
|
||||||
return EFFECT_AUTO, style, algorithm
|
|
||||||
|
|
||||||
|
mode: str
|
||||||
|
style: str
|
||||||
|
algorithm: str | None = None
|
||||||
|
|
||||||
def _get_effect(mode: str, style: str, algorithm: str | None):
|
def is_on(self, powerstate) -> bool:
|
||||||
if mode == EFFECT_MODE:
|
"""Check whether the ambilight is considered on."""
|
||||||
return f"{EFFECT_MODE}{EFFECT_PARTITION}{style}"
|
if self.mode in (EFFECT_AUTO, EFFECT_EXPERT):
|
||||||
if mode == EFFECT_EXPERT:
|
if self.style in ("FOLLOW_VIDEO", "FOLLOW_AUDIO"):
|
||||||
return f"{style}{EFFECT_PARTITION}{algorithm}{EFFECT_PARTITION}{EFFECT_EXPERT}"
|
return powerstate in ("On", None)
|
||||||
return f"{style}{EFFECT_PARTITION}{algorithm}"
|
if self.style == "OFF":
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
if self.mode == EFFECT_MODE:
|
||||||
|
if self.style == "internal":
|
||||||
|
return powerstate in ("On", None)
|
||||||
|
return True
|
||||||
|
|
||||||
def _is_on(mode, style, powerstate):
|
return False
|
||||||
if mode in (EFFECT_AUTO, EFFECT_EXPERT):
|
|
||||||
if style in ("FOLLOW_VIDEO", "FOLLOW_AUDIO"):
|
def is_valid(self) -> bool:
|
||||||
return powerstate in ("On", None)
|
"""Validate the effect configuration."""
|
||||||
if style == "OFF":
|
if self.mode == EFFECT_EXPERT:
|
||||||
return False
|
return self.style in EFFECT_EXPERT_STYLES
|
||||||
return True
|
return True
|
||||||
|
|
||||||
if mode == EFFECT_MODE:
|
@staticmethod
|
||||||
if style == "internal":
|
def from_str(effect_string: str) -> AmbilightEffect:
|
||||||
return powerstate in ("On", None)
|
"""Create AmbilightEffect object from string."""
|
||||||
return True
|
style, _, algorithm = effect_string.partition(EFFECT_PARTITION)
|
||||||
|
if style == EFFECT_MODE:
|
||||||
|
return AmbilightEffect(mode=EFFECT_MODE, style=algorithm, algorithm=None)
|
||||||
|
algorithm, _, expert = algorithm.partition(EFFECT_PARTITION)
|
||||||
|
if expert:
|
||||||
|
return AmbilightEffect(mode=EFFECT_EXPERT, style=style, algorithm=algorithm)
|
||||||
|
return AmbilightEffect(mode=EFFECT_AUTO, style=style, algorithm=algorithm)
|
||||||
|
|
||||||
return False
|
def __str__(self) -> str:
|
||||||
|
"""Get a string representation of the effect."""
|
||||||
|
if self.mode == EFFECT_MODE:
|
||||||
def _is_valid(mode, style):
|
return f"{EFFECT_MODE}{EFFECT_PARTITION}{self.style}"
|
||||||
if mode == EFFECT_EXPERT:
|
if self.mode == EFFECT_EXPERT:
|
||||||
return style in EFFECT_EXPERT_STYLES
|
return f"{self.style}{EFFECT_PARTITION}{self.algorithm}{EFFECT_PARTITION}{EFFECT_EXPERT}"
|
||||||
return True
|
return f"{self.style}{EFFECT_PARTITION}{self.algorithm}"
|
||||||
|
|
||||||
|
|
||||||
def _get_cache_keys(device: PhilipsTV):
|
def _get_cache_keys(device: PhilipsTV):
|
||||||
@ -137,6 +149,7 @@ class PhilipsTVLightEntity(
|
|||||||
self._hs = None
|
self._hs = None
|
||||||
self._brightness = None
|
self._brightness = None
|
||||||
self._cache_keys = None
|
self._cache_keys = None
|
||||||
|
self._last_selected_effect: AmbilightEffect = None
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
self._attr_supported_color_modes = [COLOR_MODE_HS, COLOR_MODE_ONOFF]
|
self._attr_supported_color_modes = [COLOR_MODE_HS, COLOR_MODE_ONOFF]
|
||||||
@ -160,48 +173,48 @@ class PhilipsTVLightEntity(
|
|||||||
|
|
||||||
def _calculate_effect_list(self):
|
def _calculate_effect_list(self):
|
||||||
"""Calculate an effect list based on current status."""
|
"""Calculate an effect list based on current status."""
|
||||||
effects = []
|
effects: list[AmbilightEffect] = []
|
||||||
effects.extend(
|
effects.extend(
|
||||||
_get_effect(EFFECT_AUTO, style, setting)
|
AmbilightEffect(mode=EFFECT_AUTO, style=style, algorithm=setting)
|
||||||
for style, data in self._tv.ambilight_styles.items()
|
for style, data in self._tv.ambilight_styles.items()
|
||||||
if _is_valid(EFFECT_AUTO, style)
|
|
||||||
and _is_on(EFFECT_AUTO, style, self._tv.powerstate)
|
|
||||||
for setting in data.get("menuSettings", [])
|
for setting in data.get("menuSettings", [])
|
||||||
)
|
)
|
||||||
|
|
||||||
effects.extend(
|
effects.extend(
|
||||||
_get_effect(EFFECT_EXPERT, style, algorithm)
|
AmbilightEffect(mode=EFFECT_EXPERT, style=style, algorithm=algorithm)
|
||||||
for style, data in self._tv.ambilight_styles.items()
|
for style, data in self._tv.ambilight_styles.items()
|
||||||
if _is_valid(EFFECT_EXPERT, style)
|
|
||||||
and _is_on(EFFECT_EXPERT, style, self._tv.powerstate)
|
|
||||||
for algorithm in data.get("algorithms", [])
|
for algorithm in data.get("algorithms", [])
|
||||||
)
|
)
|
||||||
|
|
||||||
effects.extend(
|
effects.extend(
|
||||||
_get_effect(EFFECT_MODE, style, None)
|
AmbilightEffect(mode=EFFECT_MODE, style=style)
|
||||||
for style in self._tv.ambilight_modes
|
for style in self._tv.ambilight_modes
|
||||||
if _is_valid(EFFECT_MODE, style)
|
|
||||||
and _is_on(EFFECT_MODE, style, self._tv.powerstate)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return sorted(effects)
|
filtered_effects = [
|
||||||
|
str(effect)
|
||||||
|
for effect in effects
|
||||||
|
if effect.is_valid() and effect.is_on(self._tv.powerstate)
|
||||||
|
]
|
||||||
|
|
||||||
def _calculate_effect(self):
|
return sorted(filtered_effects)
|
||||||
|
|
||||||
|
def _calculate_effect(self) -> AmbilightEffect:
|
||||||
"""Return the current effect."""
|
"""Return the current effect."""
|
||||||
current = self._tv.ambilight_current_configuration
|
current = self._tv.ambilight_current_configuration
|
||||||
if current and self._tv.ambilight_mode != "manual":
|
if current and self._tv.ambilight_mode != "manual":
|
||||||
if current["isExpert"]:
|
if current["isExpert"]:
|
||||||
if settings := _get_settings(current):
|
if settings := _get_settings(current):
|
||||||
return _get_effect(
|
return AmbilightEffect(
|
||||||
EFFECT_EXPERT, current["styleName"], settings["algorithm"]
|
EFFECT_EXPERT, current["styleName"], settings["algorithm"]
|
||||||
)
|
)
|
||||||
return _get_effect(EFFECT_EXPERT, current["styleName"], None)
|
return AmbilightEffect(EFFECT_EXPERT, current["styleName"], None)
|
||||||
|
|
||||||
return _get_effect(
|
return AmbilightEffect(
|
||||||
EFFECT_AUTO, current["styleName"], current.get("menuSetting", None)
|
EFFECT_AUTO, current["styleName"], current.get("menuSetting", None)
|
||||||
)
|
)
|
||||||
|
|
||||||
return _get_effect(EFFECT_MODE, self._tv.ambilight_mode, None)
|
return AmbilightEffect(EFFECT_MODE, self._tv.ambilight_mode, None)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def color_mode(self):
|
def color_mode(self):
|
||||||
@ -219,8 +232,8 @@ class PhilipsTVLightEntity(
|
|||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return if the light is turned on."""
|
"""Return if the light is turned on."""
|
||||||
if self._tv.on:
|
if self._tv.on:
|
||||||
mode, style, _ = _parse_effect(self.effect)
|
effect = AmbilightEffect.from_str(self.effect)
|
||||||
return _is_on(mode, style, self._tv.powerstate)
|
return effect.is_on(self._tv.powerstate)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -231,21 +244,23 @@ class PhilipsTVLightEntity(
|
|||||||
if (cache_keys := _get_cache_keys(self._tv)) != self._cache_keys:
|
if (cache_keys := _get_cache_keys(self._tv)) != self._cache_keys:
|
||||||
self._cache_keys = cache_keys
|
self._cache_keys = cache_keys
|
||||||
self._attr_effect_list = self._calculate_effect_list()
|
self._attr_effect_list = self._calculate_effect_list()
|
||||||
self._attr_effect = self._calculate_effect()
|
self._attr_effect = str(self._calculate_effect())
|
||||||
|
|
||||||
if current and current["isExpert"]:
|
if current and current["isExpert"]:
|
||||||
if settings := _get_settings(current):
|
if settings := _get_settings(current):
|
||||||
color = settings["color"]
|
color = settings["color"]
|
||||||
|
|
||||||
mode, _, _ = _parse_effect(self._attr_effect)
|
effect = AmbilightEffect.from_str(self._attr_effect)
|
||||||
|
if effect.is_on(self._tv.powerstate):
|
||||||
|
self._last_selected_effect = effect
|
||||||
|
|
||||||
if mode == EFFECT_EXPERT and color:
|
if effect.mode == EFFECT_EXPERT and color:
|
||||||
self._attr_hs_color = (
|
self._attr_hs_color = (
|
||||||
color["hue"] * 360.0 / 255.0,
|
color["hue"] * 360.0 / 255.0,
|
||||||
color["saturation"] * 100.0 / 255.0,
|
color["saturation"] * 100.0 / 255.0,
|
||||||
)
|
)
|
||||||
self._attr_brightness = color["brightness"]
|
self._attr_brightness = color["brightness"]
|
||||||
elif mode == EFFECT_MODE and self._tv.ambilight_cached:
|
elif effect.mode == EFFECT_MODE and self._tv.ambilight_cached:
|
||||||
hsv_h, hsv_s, hsv_v = color_RGB_to_hsv(
|
hsv_h, hsv_s, hsv_v = color_RGB_to_hsv(
|
||||||
*_average_pixels(self._tv.ambilight_cached)
|
*_average_pixels(self._tv.ambilight_cached)
|
||||||
)
|
)
|
||||||
@ -261,7 +276,9 @@ class PhilipsTVLightEntity(
|
|||||||
self._update_from_coordinator()
|
self._update_from_coordinator()
|
||||||
super()._handle_coordinator_update()
|
super()._handle_coordinator_update()
|
||||||
|
|
||||||
async def _set_ambilight_cached(self, algorithm, hs_color, brightness):
|
async def _set_ambilight_cached(
|
||||||
|
self, effect: AmbilightEffect, hs_color: tuple[float, float], brightness: int
|
||||||
|
):
|
||||||
"""Set ambilight via the manual or expert mode."""
|
"""Set ambilight via the manual or expert mode."""
|
||||||
rgb = color_hsv_to_RGB(hs_color[0], hs_color[1], brightness * 100 / 255)
|
rgb = color_hsv_to_RGB(hs_color[0], hs_color[1], brightness * 100 / 255)
|
||||||
|
|
||||||
@ -274,21 +291,21 @@ class PhilipsTVLightEntity(
|
|||||||
if not await self._tv.setAmbilightCached(data):
|
if not await self._tv.setAmbilightCached(data):
|
||||||
raise Exception("Failed to set ambilight color")
|
raise Exception("Failed to set ambilight color")
|
||||||
|
|
||||||
if algorithm != self._tv.ambilight_mode:
|
if effect.style != self._tv.ambilight_mode:
|
||||||
if not await self._tv.setAmbilightMode(algorithm):
|
if not await self._tv.setAmbilightMode(effect.style):
|
||||||
raise Exception("Failed to set ambilight mode")
|
raise Exception("Failed to set ambilight mode")
|
||||||
|
|
||||||
async def _set_ambilight_expert_config(
|
async def _set_ambilight_expert_config(
|
||||||
self, style, algorithm, hs_color, brightness
|
self, effect: AmbilightEffect, hs_color: tuple[float, float], brightness: int
|
||||||
):
|
):
|
||||||
"""Set ambilight via current configuration."""
|
"""Set ambilight via current configuration."""
|
||||||
config: AmbilightCurrentConfiguration = {
|
config: AmbilightCurrentConfiguration = {
|
||||||
"styleName": style,
|
"styleName": effect.style,
|
||||||
"isExpert": True,
|
"isExpert": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
setting = {
|
setting = {
|
||||||
"algorithm": algorithm,
|
"algorithm": effect.algorithm,
|
||||||
"color": {
|
"color": {
|
||||||
"hue": round(hs_color[0] * 255.0 / 360.0),
|
"hue": round(hs_color[0] * 255.0 / 360.0),
|
||||||
"saturation": round(hs_color[1] * 255.0 / 100.0),
|
"saturation": round(hs_color[1] * 255.0 / 100.0),
|
||||||
@ -301,23 +318,23 @@ class PhilipsTVLightEntity(
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if style in ("FOLLOW_COLOR", "Lounge light"):
|
if effect.style in ("FOLLOW_COLOR", "Lounge light"):
|
||||||
config["colorSettings"] = setting
|
config["colorSettings"] = setting
|
||||||
config["speed"] = 2
|
config["speed"] = 2
|
||||||
|
|
||||||
elif style == "FOLLOW_AUDIO":
|
elif effect.style == "FOLLOW_AUDIO":
|
||||||
config["audioSettings"] = setting
|
config["audioSettings"] = setting
|
||||||
config["tuning"] = 0
|
config["tuning"] = 0
|
||||||
|
|
||||||
if not await self._tv.setAmbilightCurrentConfiguration(config):
|
if not await self._tv.setAmbilightCurrentConfiguration(config):
|
||||||
raise Exception("Failed to set ambilight mode")
|
raise Exception("Failed to set ambilight mode")
|
||||||
|
|
||||||
async def _set_ambilight_config(self, style, algorithm):
|
async def _set_ambilight_config(self, effect: AmbilightEffect):
|
||||||
"""Set ambilight via current configuration."""
|
"""Set ambilight via current configuration."""
|
||||||
config: AmbilightCurrentConfiguration = {
|
config: AmbilightCurrentConfiguration = {
|
||||||
"styleName": style,
|
"styleName": effect.style,
|
||||||
"isExpert": False,
|
"isExpert": False,
|
||||||
"menuSetting": algorithm,
|
"menuSetting": effect.algorithm,
|
||||||
}
|
}
|
||||||
|
|
||||||
if await self._tv.setAmbilightCurrentConfiguration(config) is False:
|
if await self._tv.setAmbilightCurrentConfiguration(config) is False:
|
||||||
@ -327,35 +344,39 @@ class PhilipsTVLightEntity(
|
|||||||
"""Turn the bulb on."""
|
"""Turn the bulb on."""
|
||||||
brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness)
|
brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness)
|
||||||
hs_color = kwargs.get(ATTR_HS_COLOR, self.hs_color)
|
hs_color = kwargs.get(ATTR_HS_COLOR, self.hs_color)
|
||||||
effect = kwargs.get(ATTR_EFFECT, self.effect)
|
attr_effect = kwargs.get(ATTR_EFFECT, self.effect)
|
||||||
|
|
||||||
if not self._tv.on:
|
if not self._tv.on:
|
||||||
raise Exception("TV is not available")
|
raise Exception("TV is not available")
|
||||||
|
|
||||||
mode, style, setting = _parse_effect(effect)
|
effect = AmbilightEffect.from_str(attr_effect)
|
||||||
|
|
||||||
if not _is_on(mode, style, self._tv.powerstate):
|
if effect.style == "OFF":
|
||||||
mode = EFFECT_MODE
|
if self._last_selected_effect:
|
||||||
setting = None
|
effect = self._last_selected_effect
|
||||||
if self._tv.powerstate in ("On", None):
|
|
||||||
style = "internal"
|
|
||||||
else:
|
else:
|
||||||
style = "manual"
|
effect = AmbilightEffect(EFFECT_AUTO, "FOLLOW_VIDEO", "STANDARD")
|
||||||
|
|
||||||
|
if not effect.is_on(self._tv.powerstate):
|
||||||
|
effect.mode = EFFECT_MODE
|
||||||
|
effect.algorithm = None
|
||||||
|
if self._tv.powerstate in ("On", None):
|
||||||
|
effect.style = "internal"
|
||||||
|
else:
|
||||||
|
effect.style = "manual"
|
||||||
|
|
||||||
if brightness is None:
|
if brightness is None:
|
||||||
brightness = 255
|
brightness = 255
|
||||||
|
|
||||||
if hs_color is None:
|
if hs_color is None:
|
||||||
hs_color = [0, 0]
|
hs_color = (0, 0)
|
||||||
|
|
||||||
if mode == EFFECT_MODE:
|
if effect.mode == EFFECT_MODE:
|
||||||
await self._set_ambilight_cached(style, hs_color, brightness)
|
await self._set_ambilight_cached(effect, hs_color, brightness)
|
||||||
elif mode == EFFECT_AUTO:
|
elif effect.mode == EFFECT_AUTO:
|
||||||
await self._set_ambilight_config(style, setting)
|
await self._set_ambilight_config(effect)
|
||||||
elif mode == EFFECT_EXPERT:
|
elif effect.mode == EFFECT_EXPERT:
|
||||||
await self._set_ambilight_expert_config(
|
await self._set_ambilight_expert_config(effect, hs_color, brightness)
|
||||||
style, setting, hs_color, brightness
|
|
||||||
)
|
|
||||||
|
|
||||||
self._update_from_coordinator()
|
self._update_from_coordinator()
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
@ -369,7 +390,7 @@ class PhilipsTVLightEntity(
|
|||||||
if await self._tv.setAmbilightMode("internal") is False:
|
if await self._tv.setAmbilightMode("internal") is False:
|
||||||
raise Exception("Failed to set ambilight mode")
|
raise Exception("Failed to set ambilight mode")
|
||||||
|
|
||||||
await self._set_ambilight_config("OFF", "")
|
await self._set_ambilight_config(AmbilightEffect(EFFECT_MODE, "OFF", ""))
|
||||||
|
|
||||||
self._update_from_coordinator()
|
self._update_from_coordinator()
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user