Skip to content

Commit 80cefc7

Browse files
authored
Update rf-protocols to 4.0.0 (home-assistant#172131)
1 parent 2f33b4b commit 80cefc7

10 files changed

Lines changed: 40 additions & 84 deletions

File tree

homeassistant/components/novy_cooker_hood/commands.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

homeassistant/components/novy_cooker_hood/config_flow.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44
from typing import Any
55

6-
from rf_protocols.codes.novy.cooker_hood import get_codes_for_code
6+
from rf_protocols.codes.novy.cooker_hood import NovyCookerHoodButton
77
import voluptuous as vol
88

99
from homeassistant.components.radio_frequency import (
@@ -19,7 +19,6 @@
1919
from homeassistant.exceptions import HomeAssistantError
2020
from homeassistant.helpers import entity_registry as er, selector
2121

22-
from .commands import COMMAND_LIGHT
2322
from .const import (
2423
CODE_MAX,
2524
CODE_MIN,
@@ -128,10 +127,8 @@ async def async_step_test_light(
128127
) -> ConfigFlowResult:
129128
"""Toggle the hood light on then off so it ends in its starting state."""
130129
assert self._transmitter_entity_id is not None
130+
command = NovyCookerHoodButton.LIGHT.to_command(channel=self._code)
131131
try:
132-
command = await get_codes_for_code(self._code).async_load_command(
133-
COMMAND_LIGHT
134-
)
135132
await async_send_command(self.hass, self._transmitter_entity_id, command)
136133
await asyncio.sleep(_TOGGLE_GAP)
137134
await async_send_command(self.hass, self._transmitter_entity_id, command)

homeassistant/components/novy_cooker_hood/fan.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import math
44
from typing import Any
55

6-
from rf_protocols.codes.novy.cooker_hood import get_codes_for_code
6+
from rf_protocols.codes.novy.cooker_hood import NovyCookerHoodButton
7+
from rf_protocols.commands.novy import NovyCookerHoodCommand
78

89
from homeassistant.components.fan import ATTR_PERCENTAGE, FanEntity, FanEntityFeature
910
from homeassistant.components.radio_frequency import async_send_command
@@ -17,7 +18,6 @@
1718
ranged_value_to_percentage,
1819
)
1920

20-
from .commands import COMMAND_MINUS, COMMAND_PLUS
2121
from .const import SPEED_COUNT
2222
from .entity import NovyCookerHoodEntity
2323

@@ -49,7 +49,7 @@ class NovyCookerHoodFan(NovyCookerHoodEntity, FanEntity, RestoreEntity):
4949
def __init__(self, entry: ConfigEntry) -> None:
5050
"""Initialize the fan."""
5151
super().__init__(entry)
52-
self._codes = get_codes_for_code(entry.data[CONF_CODE])
52+
self._code: int = entry.data[CONF_CODE]
5353
self._level = 0
5454
self._attr_unique_id = entry.entry_id
5555

@@ -103,7 +103,7 @@ async def async_set_percentage(self, percentage: int) -> None:
103103
async def async_increase_speed(self, percentage_step: int | None = None) -> None:
104104
"""Bump speed up by N hardware levels (no recalibration)."""
105105
steps = self._steps_from_percentage(percentage_step)
106-
plus = await self._codes.async_load_command(COMMAND_PLUS)
106+
plus = NovyCookerHoodButton.PLUS.to_command(channel=self._code)
107107
for _ in range(steps):
108108
await self._async_send(plus)
109109
self._level = min(SPEED_COUNT, self._level + steps)
@@ -112,7 +112,7 @@ async def async_increase_speed(self, percentage_step: int | None = None) -> None
112112
async def async_decrease_speed(self, percentage_step: int | None = None) -> None:
113113
"""Bump speed down by N hardware levels (no recalibration)."""
114114
steps = self._steps_from_percentage(percentage_step)
115-
minus = await self._codes.async_load_command(COMMAND_MINUS)
115+
minus = NovyCookerHoodButton.MINUS.to_command(channel=self._code)
116116
for _ in range(steps):
117117
await self._async_send(minus)
118118
self._level = max(0, self._level - steps)
@@ -127,17 +127,17 @@ def _steps_from_percentage(percentage_step: int | None) -> int:
127127

128128
async def _async_set_level(self, level: int) -> None:
129129
"""Reset to off with `SPEED_COUNT` minus presses, then climb to level."""
130-
minus = await self._codes.async_load_command(COMMAND_MINUS)
130+
minus = NovyCookerHoodButton.MINUS.to_command(channel=self._code)
131131
for _ in range(SPEED_COUNT):
132132
await self._async_send(minus)
133133
if level > 0:
134-
plus = await self._codes.async_load_command(COMMAND_PLUS)
134+
plus = NovyCookerHoodButton.PLUS.to_command(channel=self._code)
135135
for _ in range(level):
136136
await self._async_send(plus)
137137
self._level = level
138138
self.async_write_ha_state()
139139

140-
async def _async_send(self, command: Any) -> None:
140+
async def _async_send(self, command: NovyCookerHoodCommand) -> None:
141141
"""Send a single RF command via the configured transmitter."""
142142
await async_send_command(
143143
self.hass, self._transmitter, command, context=self._context

homeassistant/components/novy_cooker_hood/light.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import Any
44

5-
from rf_protocols.codes.novy.cooker_hood import get_codes_for_code
5+
from rf_protocols.codes.novy.cooker_hood import NovyCookerHoodButton
66

77
from homeassistant.components.light import ColorMode, LightEntity
88
from homeassistant.components.radio_frequency import async_send_command
@@ -12,7 +12,6 @@
1212
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
1313
from homeassistant.helpers.restore_state import RestoreEntity
1414

15-
from .commands import COMMAND_LIGHT
1615
from .entity import NovyCookerHoodEntity
1716

1817
PARALLEL_UPDATES = 1
@@ -37,7 +36,7 @@ class NovyCookerHoodLight(NovyCookerHoodEntity, LightEntity, RestoreEntity):
3736
def __init__(self, entry: ConfigEntry) -> None:
3837
"""Initialize the light."""
3938
super().__init__(entry)
40-
self._codes = get_codes_for_code(entry.data[CONF_CODE])
39+
self._code = entry.data[CONF_CODE]
4140
self._attr_unique_id = entry.entry_id
4241

4342
async def async_added_to_hass(self) -> None:
@@ -48,19 +47,19 @@ async def async_added_to_hass(self) -> None:
4847

4948
async def async_turn_on(self, **kwargs: Any) -> None:
5049
"""Turn the light on by sending the toggle command."""
51-
await self._async_send_command(COMMAND_LIGHT)
50+
await self._async_send_light()
5251
self._attr_is_on = True
5352
self.async_write_ha_state()
5453

5554
async def async_turn_off(self, **kwargs: Any) -> None:
5655
"""Turn the light off by sending the toggle command."""
57-
await self._async_send_command(COMMAND_LIGHT)
56+
await self._async_send_light()
5857
self._attr_is_on = False
5958
self.async_write_ha_state()
6059

61-
async def _async_send_command(self, name: str) -> None:
62-
"""Load the named command and send it via the configured transmitter."""
63-
command = await self._codes.async_load_command(name)
60+
async def _async_send_light(self) -> None:
61+
"""Send the light toggle command via the configured transmitter."""
62+
command = NovyCookerHoodButton.LIGHT.to_command(channel=self._code)
6463
await async_send_command(
6564
self.hass, self._transmitter, command, context=self._context
6665
)

homeassistant/components/radio_frequency/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"documentation": "https://www.home-assistant.io/integrations/radio_frequency",
66
"integration_type": "entity",
77
"quality_scale": "internal",
8-
"requirements": ["rf-protocols==3.2.0"]
8+
"requirements": ["rf-protocols==4.0.0"]
99
}

requirements.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements_all.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/components/novy_cooker_hood/conftest.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,18 @@
11
"""Common fixtures for the Novy Cooker Hood tests."""
22

3-
from collections.abc import Iterator
4-
from unittest.mock import AsyncMock, MagicMock, patch
5-
63
import pytest
7-
from rf_protocols.loader import CodeCollection
84

95
from homeassistant.components.novy_cooker_hood.const import CONF_TRANSMITTER, DOMAIN
106
from homeassistant.const import CONF_CODE
117
from homeassistant.core import HomeAssistant
128
from homeassistant.helpers import entity_registry as er
139

1410
from tests.common import MockConfigEntry
15-
from tests.components.radio_frequency.common import (
16-
MockRadioFrequencyCommand,
17-
MockRadioFrequencyEntity,
18-
)
11+
from tests.components.radio_frequency.common import MockRadioFrequencyEntity
1912

2013
TRANSMITTER_ENTITY_ID = "radio_frequency.test_rf_transmitter"
2114

2215

23-
@pytest.fixture(autouse=True)
24-
def mock_get_codes() -> Iterator[MagicMock]:
25-
"""Patch the bundled-codes loader so tests don't hit the filesystem."""
26-
fake_collection = MagicMock(spec=CodeCollection)
27-
fake_collection.async_load_command = AsyncMock(
28-
side_effect=lambda name: MockRadioFrequencyCommand()
29-
)
30-
with (
31-
patch(
32-
"homeassistant.components.novy_cooker_hood.light.get_codes_for_code",
33-
return_value=fake_collection,
34-
),
35-
patch(
36-
"homeassistant.components.novy_cooker_hood.fan.get_codes_for_code",
37-
return_value=fake_collection,
38-
),
39-
patch(
40-
"homeassistant.components.novy_cooker_hood.config_flow.get_codes_for_code",
41-
return_value=fake_collection,
42-
),
43-
):
44-
yield fake_collection
45-
46-
4716
@pytest.fixture
4817
def mock_config_entry(
4918
mock_rf_entity: MockRadioFrequencyEntity,

tests/components/novy_cooker_hood/test_config_flow.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Test the Novy Hood config flow."""
22

33
from collections.abc import Iterator
4-
from unittest.mock import MagicMock, patch
4+
from unittest.mock import patch
55

66
import pytest
7+
from rf_protocols.codes.novy.cooker_hood import NovyCookerHoodButton
78

8-
from homeassistant.components.novy_cooker_hood.commands import COMMAND_LIGHT
99
from homeassistant.components.novy_cooker_hood.const import CONF_TRANSMITTER, DOMAIN
1010
from homeassistant.components.radio_frequency import DATA_COMPONENT, DOMAIN as RF_DOMAIN
1111
from homeassistant.config_entries import SOURCE_USER
@@ -49,7 +49,6 @@ async def _start_user_flow(hass: HomeAssistant, code: str = "1") -> dict:
4949

5050
async def test_user_flow_test_then_finish(
5151
hass: HomeAssistant,
52-
mock_get_codes: MagicMock,
5352
mock_rf_entity: MockRadioFrequencyEntity,
5453
entity_registry: er.EntityRegistry,
5554
) -> None:
@@ -58,8 +57,10 @@ async def test_user_flow_test_then_finish(
5857

5958
assert result["type"] is FlowResultType.MENU
6059
assert result["step_id"] == "test_light"
61-
mock_get_codes.async_load_command.assert_awaited_with(COMMAND_LIGHT)
6260
assert len(mock_rf_entity.send_command_calls) == 2
61+
sent = mock_rf_entity.send_command_calls[0].command
62+
assert sent.key == NovyCookerHoodButton.LIGHT.code
63+
assert sent.channel == 3
6364

6465
result = await hass.config_entries.flow.async_configure(
6566
result["flow_id"], user_input={"next_step_id": "finish"}
@@ -77,7 +78,6 @@ async def test_user_flow_test_then_finish(
7778

7879
async def test_user_flow_retry_picks_different_code(
7980
hass: HomeAssistant,
80-
mock_get_codes: MagicMock,
8181
mock_rf_entity: MockRadioFrequencyEntity,
8282
entity_registry: er.EntityRegistry,
8383
) -> None:
@@ -99,9 +99,13 @@ async def test_user_flow_retry_picks_different_code(
9999
},
100100
)
101101
assert result["type"] is FlowResultType.MENU
102-
# One load per test x two tests; two sends per test x two tests.
103-
assert mock_get_codes.async_load_command.await_count == 2
104102
assert len(mock_rf_entity.send_command_calls) == 4
103+
assert [c.command.channel for c in mock_rf_entity.send_command_calls] == [
104+
1,
105+
1,
106+
7,
107+
7,
108+
]
105109

106110
result = await hass.config_entries.flow.async_configure(
107111
result["flow_id"], user_input={"next_step_id": "finish"}
@@ -127,7 +131,6 @@ async def test_user_flow_test_transmit_failure(
127131

128132
async def test_recover_after_transmit_failure(
129133
hass: HomeAssistant,
130-
mock_get_codes: MagicMock,
131134
mock_rf_entity: MockRadioFrequencyEntity,
132135
) -> None:
133136
"""The user can Retry from test_failed and complete the flow."""
@@ -183,7 +186,6 @@ async def test_unique_id_already_configured(
183186

184187
async def test_same_transmitter_different_code_is_allowed(
185188
hass: HomeAssistant,
186-
mock_get_codes: MagicMock,
187189
mock_config_entry: MockConfigEntry,
188190
mock_rf_entity: MockRadioFrequencyEntity,
189191
entity_registry: er.EntityRegistry,
@@ -205,7 +207,6 @@ async def test_same_transmitter_different_code_is_allowed(
205207

206208
async def test_reconfigure_updates_entry(
207209
hass: HomeAssistant,
208-
mock_get_codes: MagicMock,
209210
init_novy_cooker_hood: MockConfigEntry,
210211
mock_rf_entity: MockRadioFrequencyEntity,
211212
entity_registry: er.EntityRegistry,
@@ -224,7 +225,9 @@ async def test_reconfigure_updates_entry(
224225
)
225226
assert result["type"] is FlowResultType.MENU
226227
assert result["step_id"] == "test_light"
227-
mock_get_codes.async_load_command.assert_awaited_with(COMMAND_LIGHT)
228+
sent = mock_rf_entity.send_command_calls[-1].command
229+
assert sent.key == NovyCookerHoodButton.LIGHT.code
230+
assert sent.channel == 4
228231

229232
result = await hass.config_entries.flow.async_configure(
230233
result["flow_id"], user_input={"next_step_id": "finish"}
@@ -239,7 +242,6 @@ async def test_reconfigure_updates_entry(
239242

240243
async def test_reconfigure_frees_old_unique_id(
241244
hass: HomeAssistant,
242-
mock_get_codes: MagicMock,
243245
init_novy_cooker_hood: MockConfigEntry,
244246
mock_rf_entity: MockRadioFrequencyEntity,
245247
) -> None:
@@ -295,7 +297,6 @@ async def test_reconfigure_aborts_on_collision(
295297

296298
async def test_reconfigure_retry_returns_to_picker(
297299
hass: HomeAssistant,
298-
mock_get_codes: MagicMock,
299300
init_novy_cooker_hood: MockConfigEntry,
300301
mock_rf_entity: MockRadioFrequencyEntity,
301302
) -> None:
@@ -326,7 +327,6 @@ async def test_no_transmitters(hass: HomeAssistant) -> None:
326327

327328
async def test_recover_after_no_transmitters(
328329
hass: HomeAssistant,
329-
mock_get_codes: MagicMock,
330330
) -> None:
331331
"""User can re-init the flow after the radio_frequency integration loads."""
332332
result = await hass.config_entries.flow.async_init(

tests/components/novy_cooker_hood/test_light.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""Tests for the Novy Hood light platform."""
22

3-
from unittest.mock import MagicMock, call
3+
from rf_protocols.codes.novy.cooker_hood import NovyCookerHoodButton
44

55
from homeassistant.components.light import (
66
DOMAIN as LIGHT_DOMAIN,
77
SERVICE_TURN_OFF,
88
SERVICE_TURN_ON,
99
)
10-
from homeassistant.components.novy_cooker_hood.commands import COMMAND_LIGHT
1110
from homeassistant.const import (
1211
ATTR_ASSUMED_STATE,
1312
ATTR_ENTITY_ID,
@@ -28,7 +27,6 @@
2827

2928
async def test_turn_on_and_off_send_light_once_each(
3029
hass: HomeAssistant,
31-
mock_get_codes: MagicMock,
3230
mock_rf_entity: MockRadioFrequencyEntity,
3331
init_novy_cooker_hood: MockConfigEntry,
3432
) -> None:
@@ -66,11 +64,11 @@ async def test_turn_on_and_off_send_light_once_each(
6664
state = hass.states.get(ENTITY_ID)
6765
assert state is not None
6866
assert state.state == STATE_OFF
69-
assert mock_get_codes.async_load_command.await_args_list == [
70-
call(COMMAND_LIGHT),
71-
call(COMMAND_LIGHT),
72-
]
7367
assert len(mock_rf_entity.send_command_calls) == 2
68+
assert [c.command.key for c in mock_rf_entity.send_command_calls] == [
69+
NovyCookerHoodButton.LIGHT.code,
70+
NovyCookerHoodButton.LIGHT.code,
71+
]
7472

7573

7674
async def test_restore_state(

0 commit comments

Comments
 (0)