Migrate the Lyngdorf remote platform to the lyngdorf 2.0 API (#180633)

This commit is contained in:
Alex Fishlock
2026-08-30 16:42:08 +02:00
committed by GitHub
parent fdea99927b
commit d6cc4252fa
3 changed files with 25 additions and 20 deletions
+15 -10
View File
@@ -3,8 +3,7 @@
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, override
from lyngdorf.device import Receiver
from lyngdorf.exceptions import LyngdorfUnsupportedError
from lyngdorf import LyngdorfReceiver, LyngdorfUnsupportedError, Remote
from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
from homeassistant.core import HomeAssistant
@@ -29,7 +28,7 @@ async def async_setup_entry(
receiver = runtime_data.receiver
# The TDAI family has no remote keys at all, so it gets no remote entity.
if not receiver.has_remote_keys:
if receiver.remote is None:
return
async_add_entities(
@@ -44,7 +43,7 @@ class LyngdorfRemote(LyngdorfEntity, RemoteEntity):
def __init__(
self,
receiver: Receiver,
receiver: LyngdorfReceiver,
config_entry: LyngdorfConfigEntry,
device_info: DeviceInfo,
) -> None:
@@ -54,6 +53,14 @@ class LyngdorfRemote(LyngdorfEntity, RemoteEntity):
assert config_entry.unique_id
self._attr_unique_id = config_entry.unique_id
@property
def _remote(self) -> Remote:
"""Return the remote; this entity exists only when the model has one."""
remote = self._receiver.remote
if TYPE_CHECKING:
assert remote is not None
return remote
@override
@property
def is_on(self) -> bool | None:
@@ -63,24 +70,22 @@ class LyngdorfRemote(LyngdorfEntity, RemoteEntity):
@override
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the device on."""
self._receiver.power_on = True
await self._receiver.set_power(True)
@override
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
self._receiver.power_on = False
await self._receiver.set_power(False)
@override
async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
"""Send a sequence of remote keys to the device."""
# delay_secs is dropped: the library already paces its own writes.
try:
self._receiver.send_remote_commands(
command, num_repeats=kwargs[ATTR_NUM_REPEATS]
)
await self._remote.send(command, num_repeats=kwargs[ATTR_NUM_REPEATS])
except LyngdorfUnsupportedError as err:
# The member value is what a caller sends: DIGIT_0 is "0", not "digit_0".
keys = sorted(key.value for key in self._receiver.available_remote_keys)
keys = sorted(key.value for key in self._remote.keys)
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="unsupported_remote_key",
+4
View File
@@ -11,6 +11,7 @@ from lyngdorf import (
LyngdorfReceiver,
NumericControl,
NumericRange,
Remote,
RemoteKey,
Trim,
ZoneB,
@@ -104,6 +105,9 @@ def mock_receiver(mock_create_receiver: MagicMock) -> MagicMock:
RemoteKey.DIGIT_0,
}
)
remote = MagicMock(spec=Remote)
remote.keys = receiver.available_remote_keys
receiver.remote = remote
# Diagnostics reports the whole receiver, so every property it reads
# needs a value here; an unset one is a mock the response cannot encode.
+6 -10
View File
@@ -2,7 +2,7 @@
from unittest.mock import MagicMock, patch
from lyngdorf.const import LyngdorfModel
from lyngdorf import LyngdorfModel
from lyngdorf.exceptions import LyngdorfUnsupportedError
from lyngdorf.remote import RemoteKey, resolve_remote_key
import pytest
@@ -61,7 +61,7 @@ async def test_send_command(
blocking=True,
)
mock_receiver.send_remote_commands.assert_called_once_with(
mock_receiver.remote.send.assert_awaited_once_with(
[RemoteKey.MENU, RemoteKey.DOWN, RemoteKey.ENTER], num_repeats=1
)
@@ -83,9 +83,7 @@ async def test_send_command_repeats(
blocking=True,
)
mock_receiver.send_remote_commands.assert_called_once_with(
[RemoteKey.DOWN], num_repeats=3
)
mock_receiver.remote.send.assert_awaited_once_with([RemoteKey.DOWN], num_repeats=3)
@pytest.mark.usefixtures("init_integration")
@@ -94,9 +92,7 @@ async def test_send_unsupported_command(
mock_receiver: MagicMock,
) -> None:
"""Test a key the model does not have is reported to the user."""
mock_receiver.send_remote_commands.side_effect = LyngdorfUnsupportedError(
"no such key"
)
mock_receiver.remote.send.side_effect = LyngdorfUnsupportedError("no such key")
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
@@ -135,7 +131,7 @@ async def test_power(
blocking=True,
)
assert mock_receiver.power_on is expected
mock_receiver.set_power.assert_awaited_once_with(expected)
@pytest.mark.usefixtures("mock_receiver")
@@ -146,7 +142,7 @@ async def test_no_entity_for_model_without_remote_keys(
entity_registry: er.EntityRegistry,
) -> None:
"""Test no remote entity is created for a model with no remote keys."""
mock_receiver.has_remote_keys = False
mock_receiver.remote = None
mock_config_entry.add_to_hass(hass)
with (