mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Send Broadlink RM4 Pro RF commands on the requested frequency (#193181)
This commit is contained in:
@@ -4,7 +4,7 @@ import logging
|
||||
from typing import override
|
||||
|
||||
from broadlink_async.exceptions import BroadlinkException
|
||||
from broadlink_async.remote import TICK
|
||||
from broadlink_async.remote import TICK, pulses_to_rm4_rf
|
||||
from rf_protocols import RadioFrequencyCommand
|
||||
|
||||
from homeassistant.components.radio_frequency import RadioFrequencyTransmitterEntity
|
||||
@@ -27,6 +27,9 @@ _TICK_US = TICK
|
||||
|
||||
_MAX_TICKS = 0xFFFF # About 2 s
|
||||
|
||||
# Off time after a frame that ends on a pulse, as in a learned RM4 Pro code.
|
||||
_RM4_RF_GAP_US = 12_881
|
||||
|
||||
_RF_433_TYPE_BYTE = 0xB2
|
||||
_RF_315_TYPE_BYTE = 0xB4
|
||||
|
||||
@@ -90,6 +93,31 @@ def encode_rf_packet(
|
||||
return bytes(buf)
|
||||
|
||||
|
||||
def encode_rm4_rf_packet(
|
||||
*,
|
||||
frequency: int,
|
||||
repeat_count: int,
|
||||
timings_us: list[int],
|
||||
) -> bytes:
|
||||
"""Encode raw OOK timings as an RM4 Pro RF packet that carries its frequency.
|
||||
|
||||
The frame is written out ``repeat_count + 1`` times.
|
||||
"""
|
||||
gap_us = _RM4_RF_GAP_US if timings_us and timings_us[-1] > 0 else None
|
||||
try:
|
||||
return pulses_to_rm4_rf(
|
||||
timings_us,
|
||||
frequency / 1_000_000,
|
||||
frames=repeat_count + 1,
|
||||
gap_us=gap_us,
|
||||
)
|
||||
except ValueError as err:
|
||||
raise HomeAssistantError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="invalid_rf_timings",
|
||||
) from err
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
@@ -120,12 +148,18 @@ class BroadlinkRadioFrequency(BroadlinkEntity, RadioFrequencyTransmitterEntity):
|
||||
@override
|
||||
async def async_send_command(self, command: RadioFrequencyCommand) -> None:
|
||||
"""Encode an OOK command and transmit it via the Broadlink device."""
|
||||
type_byte = _type_byte_for_frequency(command.frequency)
|
||||
packet = encode_rf_packet(
|
||||
type_byte=type_byte,
|
||||
repeat_count=command.repeat_count,
|
||||
timings_us=command.get_raw_timings(),
|
||||
)
|
||||
if self._device.api.type == "RM4PRO":
|
||||
packet = encode_rm4_rf_packet(
|
||||
frequency=command.frequency,
|
||||
repeat_count=command.repeat_count,
|
||||
timings_us=command.get_raw_timings(),
|
||||
)
|
||||
else:
|
||||
packet = encode_rf_packet(
|
||||
type_byte=_type_byte_for_frequency(command.frequency),
|
||||
repeat_count=command.repeat_count,
|
||||
timings_us=command.get_raw_timings(),
|
||||
)
|
||||
_LOGGER.debug(
|
||||
"Transmitting RF packet: %d bytes on %d Hz (repeat=%d)",
|
||||
len(packet),
|
||||
|
||||
@@ -113,6 +113,9 @@
|
||||
"invalid_command": {
|
||||
"message": "Invalid command: {error}"
|
||||
},
|
||||
"invalid_rf_timings": {
|
||||
"message": "Broadlink devices cannot transmit these RF timings"
|
||||
},
|
||||
"learn_command_failed": {
|
||||
"message": "Failed to learn command {command}: {error}"
|
||||
},
|
||||
|
||||
@@ -28,9 +28,11 @@ _FREQ_433 = 433_920_000
|
||||
_FREQ_315 = 315_000_000
|
||||
|
||||
|
||||
async def _setup_rf_device(hass: HomeAssistant) -> tuple[MagicMock, str]:
|
||||
"""Set up the RMPRO test device, return its api mock and RF entity_id."""
|
||||
device = get_device("Office")
|
||||
async def _setup_rf_device(
|
||||
hass: HomeAssistant, device_name: str = "Office"
|
||||
) -> tuple[MagicMock, str]:
|
||||
"""Set up a test device (RMPRO by default), return its api mock and RF entity_id."""
|
||||
device = get_device(device_name)
|
||||
mock_setup = await device.setup_entry(hass)
|
||||
entity_registry = er.async_get(hass)
|
||||
device_registry = dr.async_get(hass)
|
||||
@@ -136,6 +138,65 @@ async def test_send_command_315_band(hass: HomeAssistant) -> None:
|
||||
assert api.send_data.call_args.args[0][0] == _RF_315_TYPE_BYTE
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("frequency", "timings", "repeat_count", "expected"),
|
||||
[
|
||||
pytest.param(
|
||||
_FREQ_433,
|
||||
[400, -800, 400, -800],
|
||||
0,
|
||||
"b1c00800009f06000d1a0d1a",
|
||||
id="433_92",
|
||||
),
|
||||
pytest.param(433_420_000, [400, -800], 0, "b1c006000c9d06000d1a", id="433_42"),
|
||||
pytest.param(_FREQ_315, [400, -800], 0, "b1c0060078ce04000d1a", id="315"),
|
||||
pytest.param(
|
||||
_FREQ_433,
|
||||
[400, -800, 400, -5000],
|
||||
3,
|
||||
"b1c01400009f06000d1a0da40d1a0da40d1a0da40d1a0da4",
|
||||
id="repeat_count_keeps_own_gap",
|
||||
),
|
||||
pytest.param(
|
||||
_FREQ_433,
|
||||
[400, -800, 400],
|
||||
1,
|
||||
"b1c01000009f06000d1a0d0001a70d1a0d0001a7",
|
||||
id="ends_on_pulse",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_send_command_rm4_pro(
|
||||
hass: HomeAssistant,
|
||||
frequency: int,
|
||||
timings: list[int],
|
||||
repeat_count: int,
|
||||
expected: str,
|
||||
) -> None:
|
||||
"""An RM4 Pro packet carries the frequency and writes out each repeat as a frame."""
|
||||
api, entity_id = await _setup_rf_device(hass, "Garage")
|
||||
|
||||
command = OOKCommand(
|
||||
frequency=frequency, timings=timings, repeat_count=repeat_count
|
||||
)
|
||||
await radio_frequency.async_send_command(hass, entity_id, command)
|
||||
|
||||
assert api.send_data.call_args == call(bytes.fromhex(expected))
|
||||
|
||||
|
||||
async def test_send_command_rm4_pro_rejects_invalid_timings(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Timings that do not start with a pulse are rejected before send."""
|
||||
api, entity_id = await _setup_rf_device(hass, "Garage")
|
||||
|
||||
command = OOKCommand(frequency=_FREQ_433, timings=[-400, 800])
|
||||
with pytest.raises(HomeAssistantError) as exc_info:
|
||||
await radio_frequency.async_send_command(hass, entity_id, command)
|
||||
assert exc_info.value.translation_key == "invalid_rf_timings"
|
||||
api.send_data.assert_not_called()
|
||||
|
||||
|
||||
async def test_send_command_rejects_out_of_band(hass: HomeAssistant) -> None:
|
||||
"""An out-of-band frequency is rejected before send."""
|
||||
api, entity_id = await _setup_rf_device(hass)
|
||||
|
||||
Reference in New Issue
Block a user