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
This commit is contained in:
Claude
2026-06-12 12:07:38 +00:00
parent fdc2442cb2
commit fc60d48a17
4 changed files with 30 additions and 4 deletions
+1 -1
View File
@@ -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"]
}
+9 -2
View File
@@ -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))
)
+1 -1
View File
@@ -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
+19
View File
@@ -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"),
[