From fc60d48a171e26af88317eb2fdef19bc7ab79a66 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 12 Jun 2026 12:07:38 +0000 Subject: [PATCH] Gate Yoto auto-brightness switches on the light sensor capability Automatic display brightness requires the ambient light sensor, which only some player models have. Bump yoto-api to 4.2.0 for the new capability flag. https://claude.ai/code/session_015G3sygJ3js3qaMvw7Kn2Jo --- homeassistant/components/yoto/manifest.json | 2 +- homeassistant/components/yoto/switch.py | 11 +++++++++-- requirements_all.txt | 2 +- tests/components/yoto/test_switch.py | 19 +++++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/yoto/manifest.json b/homeassistant/components/yoto/manifest.json index c84bc1b0cce5..6f2ec44c0b81 100644 --- a/homeassistant/components/yoto/manifest.json +++ b/homeassistant/components/yoto/manifest.json @@ -10,5 +10,5 @@ "iot_class": "cloud_push", "loggers": ["yoto_api"], "quality_scale": "bronze", - "requirements": ["yoto-api==4.0.2"] + "requirements": ["yoto-api==4.2.0"] } diff --git a/homeassistant/components/yoto/switch.py b/homeassistant/components/yoto/switch.py index bde695f2ddd3..ed1d14cffecd 100644 --- a/homeassistant/components/yoto/switch.py +++ b/homeassistant/components/yoto/switch.py @@ -4,7 +4,7 @@ from collections.abc import Callable from dataclasses import dataclass from typing import Any -from yoto_api import PlayerConfig, YotoPlayer +from yoto_api import Capabilities, PlayerConfig, YotoPlayer, caps_for from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription from homeassistant.const import EntityCategory @@ -32,12 +32,14 @@ class YotoSwitchEntityDescription(SwitchEntityDescription): """Describes a Yoto switch entity. The turn_on/turn_off callables return the ``set_player_config`` kwargs - that put the player in the requested state. + that put the player in the requested state. ``supported_fn`` gates + entity creation on the player model's capabilities. """ is_on_fn: Callable[[PlayerConfig], bool | None] turn_on_fn: Callable[[PlayerConfig], dict[str, Any]] turn_off_fn: Callable[[PlayerConfig], dict[str, Any]] + supported_fn: Callable[[Capabilities], bool] = lambda caps: True SWITCHES: tuple[YotoSwitchEntityDescription, ...] = ( @@ -107,6 +109,8 @@ SWITCHES: tuple[YotoSwitchEntityDescription, ...] = ( turn_on_fn=lambda config: {"pause_power_button": True}, turn_off_fn=lambda config: {"pause_power_button": False}, ), + # Automatic display brightness needs the ambient light sensor, which + # only some player models have. YotoSwitchEntityDescription( key="day_auto_brightness", translation_key="day_auto_brightness", @@ -117,6 +121,7 @@ SWITCHES: tuple[YotoSwitchEntityDescription, ...] = ( "day_display_brightness": config.day_display_brightness or DEFAULT_DISPLAY_BRIGHTNESS }, + supported_fn=lambda caps: caps.has_light_sensor, ), YotoSwitchEntityDescription( key="night_auto_brightness", @@ -128,6 +133,7 @@ SWITCHES: tuple[YotoSwitchEntityDescription, ...] = ( "night_display_brightness": config.night_display_brightness or DEFAULT_DISPLAY_BRIGHTNESS }, + supported_fn=lambda caps: caps.has_light_sensor, ), ) @@ -143,6 +149,7 @@ async def async_setup_entry( YotoSwitch(coordinator, player, description) for player in coordinator.client.players.values() for description in SWITCHES + if description.supported_fn(caps_for(player.device)) ) diff --git a/requirements_all.txt b/requirements_all.txt index 836454148df1..96913bf84e2f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -3436,7 +3436,7 @@ yeelightsunflower==0.0.10 yolink-api==0.6.5 # homeassistant.components.yoto -yoto-api==4.0.2 +yoto-api==4.2.0 # homeassistant.components.youless youless-api==2.2.0 diff --git a/tests/components/yoto/test_switch.py b/tests/components/yoto/test_switch.py index 0acc0966b480..b2199355ae6b 100644 --- a/tests/components/yoto/test_switch.py +++ b/tests/components/yoto/test_switch.py @@ -1,5 +1,6 @@ """Tests for the Yoto switch platform.""" +from dataclasses import replace from unittest.mock import MagicMock, patch import pytest @@ -43,6 +44,24 @@ async def test_all_entities( await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) +@pytest.mark.parametrize("device_family", ["mini", "v2"]) +async def test_no_auto_brightness_without_light_sensor( + hass: HomeAssistant, + mock_yoto_client: MagicMock, + mock_config_entry: MockConfigEntry, + device_family: str, +) -> None: + """Models without an ambient light sensor get no auto-brightness switches.""" + player = mock_yoto_client.players[PLAYER_ID] + player.device = replace(player.device, device_family=device_family) + + await _setup(hass, mock_config_entry) + + assert hass.states.get("switch.nursery_yoto_day_automatic_brightness") is None + assert hass.states.get("switch.nursery_yoto_night_automatic_brightness") is None + assert hass.states.get("switch.nursery_yoto_bluetooth") is not None + + @pytest.mark.parametrize( ("entity_id", "service", "expected_fields"), [