Add monkeypatch type hints to rflink tests (#121053)

This commit is contained in:
epenet 2024-07-05 10:54:16 +02:00 committed by GitHub
parent 0cf5b4f965
commit 2ab02c06c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 178 additions and 59 deletions

View file

@ -7,6 +7,7 @@ automatic sensor creation.
from datetime import timedelta from datetime import timedelta
from freezegun import freeze_time from freezegun import freeze_time
import pytest
from homeassistant.components.rflink import CONF_RECONNECT_INTERVAL from homeassistant.components.rflink import CONF_RECONNECT_INTERVAL
from homeassistant.const import ( from homeassistant.const import (
@ -45,7 +46,9 @@ CONFIG = {
} }
async def test_default_setup(hass: HomeAssistant, monkeypatch) -> None: async def test_default_setup(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test all basic functionality of the rflink sensor component.""" """Test all basic functionality of the rflink sensor component."""
# setup mocking rflink module # setup mocking rflink module
event_callback, create, _, _ = await mock_rflink(hass, CONFIG, DOMAIN, monkeypatch) event_callback, create, _, _ = await mock_rflink(hass, CONFIG, DOMAIN, monkeypatch)
@ -84,7 +87,9 @@ async def test_default_setup(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get("binary_sensor.test").state == STATE_OFF assert hass.states.get("binary_sensor.test").state == STATE_OFF
async def test_entity_availability(hass: HomeAssistant, monkeypatch) -> None: async def test_entity_availability(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""If Rflink device is disconnected, entities should become unavailable.""" """If Rflink device is disconnected, entities should become unavailable."""
# Make sure Rflink mock does not 'recover' to quickly from the # Make sure Rflink mock does not 'recover' to quickly from the
# disconnect or else the unavailability cannot be measured # disconnect or else the unavailability cannot be measured
@ -125,7 +130,7 @@ async def test_entity_availability(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get("binary_sensor.test").state == STATE_ON assert hass.states.get("binary_sensor.test").state == STATE_ON
async def test_off_delay(hass: HomeAssistant, monkeypatch) -> None: async def test_off_delay(hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test off_delay option.""" """Test off_delay option."""
# setup mocking rflink module # setup mocking rflink module
event_callback, create, _, _ = await mock_rflink(hass, CONFIG, DOMAIN, monkeypatch) event_callback, create, _, _ = await mock_rflink(hass, CONFIG, DOMAIN, monkeypatch)
@ -188,7 +193,9 @@ async def test_off_delay(hass: HomeAssistant, monkeypatch) -> None:
assert len(events) == 3 assert len(events) == 3
async def test_restore_state(hass: HomeAssistant, monkeypatch) -> None: async def test_restore_state(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Ensure states are restored on startup.""" """Ensure states are restored on startup."""
mock_restore_cache( mock_restore_cache(
hass, (State(f"{DOMAIN}.test", STATE_ON), State(f"{DOMAIN}.test2", STATE_ON)) hass, (State(f"{DOMAIN}.test", STATE_ON), State(f"{DOMAIN}.test2", STATE_ON))

View file

@ -5,6 +5,8 @@ control of RFLink cover devices.
""" """
import pytest
from homeassistant.components.rflink import EVENT_BUTTON_PRESSED from homeassistant.components.rflink import EVENT_BUTTON_PRESSED
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
@ -37,7 +39,9 @@ CONFIG = {
} }
async def test_default_setup(hass: HomeAssistant, monkeypatch) -> None: async def test_default_setup(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test all basic functionality of the RFLink cover component.""" """Test all basic functionality of the RFLink cover component."""
# setup mocking rflink module # setup mocking rflink module
event_callback, create, protocol, _ = await mock_rflink( event_callback, create, protocol, _ = await mock_rflink(
@ -107,7 +111,9 @@ async def test_default_setup(hass: HomeAssistant, monkeypatch) -> None:
assert protocol.send_command_ack.call_args_list[1][0][1] == "UP" assert protocol.send_command_ack.call_args_list[1][0][1] == "UP"
async def test_firing_bus_event(hass: HomeAssistant, monkeypatch) -> None: async def test_firing_bus_event(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Incoming RFLink command events should be put on the HA event bus.""" """Incoming RFLink command events should be put on the HA event bus."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -142,7 +148,9 @@ async def test_firing_bus_event(hass: HomeAssistant, monkeypatch) -> None:
assert calls[0].data == {"state": "down", "entity_id": f"{DOMAIN}.test"} assert calls[0].data == {"state": "down", "entity_id": f"{DOMAIN}.test"}
async def test_signal_repetitions(hass: HomeAssistant, monkeypatch) -> None: async def test_signal_repetitions(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Command should be sent amount of configured repetitions.""" """Command should be sent amount of configured repetitions."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -180,7 +188,9 @@ async def test_signal_repetitions(hass: HomeAssistant, monkeypatch) -> None:
assert protocol.send_command_ack.call_count == 5 assert protocol.send_command_ack.call_count == 5
async def test_signal_repetitions_alternation(hass: HomeAssistant, monkeypatch) -> None: async def test_signal_repetitions_alternation(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Simultaneously switching entities must alternate repetitions.""" """Simultaneously switching entities must alternate repetitions."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -211,7 +221,9 @@ async def test_signal_repetitions_alternation(hass: HomeAssistant, monkeypatch)
assert protocol.send_command_ack.call_args_list[3][0][0] == "protocol_0_1" assert protocol.send_command_ack.call_args_list[3][0][0] == "protocol_0_1"
async def test_signal_repetitions_cancelling(hass: HomeAssistant, monkeypatch) -> None: async def test_signal_repetitions_cancelling(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Cancel outstanding repetitions when state changed.""" """Cancel outstanding repetitions when state changed."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -240,7 +252,9 @@ async def test_signal_repetitions_cancelling(hass: HomeAssistant, monkeypatch) -
assert protocol.send_command_ack.call_args_list[3][0][1] == "UP" assert protocol.send_command_ack.call_args_list[3][0][1] == "UP"
async def test_group_alias(hass: HomeAssistant, monkeypatch) -> None: async def test_group_alias(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Group aliases should only respond to group commands (allon/alloff).""" """Group aliases should only respond to group commands (allon/alloff)."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -270,7 +284,9 @@ async def test_group_alias(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get(f"{DOMAIN}.test").state == STATE_OPEN assert hass.states.get(f"{DOMAIN}.test").state == STATE_OPEN
async def test_nogroup_alias(hass: HomeAssistant, monkeypatch) -> None: async def test_nogroup_alias(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Non group aliases should not respond to group commands.""" """Non group aliases should not respond to group commands."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -303,7 +319,9 @@ async def test_nogroup_alias(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get(f"{DOMAIN}.test").state == STATE_OPEN assert hass.states.get(f"{DOMAIN}.test").state == STATE_OPEN
async def test_nogroup_device_id(hass: HomeAssistant, monkeypatch) -> None: async def test_nogroup_device_id(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Device id that do not respond to group commands (allon/alloff).""" """Device id that do not respond to group commands (allon/alloff)."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -331,7 +349,9 @@ async def test_nogroup_device_id(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get(f"{DOMAIN}.test").state == STATE_OPEN assert hass.states.get(f"{DOMAIN}.test").state == STATE_OPEN
async def test_restore_state(hass: HomeAssistant, monkeypatch) -> None: async def test_restore_state(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Ensure states are restored on startup.""" """Ensure states are restored on startup."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -377,7 +397,9 @@ async def test_restore_state(hass: HomeAssistant, monkeypatch) -> None:
# The code checks the ID, it will use the # The code checks the ID, it will use the
# 'inverted' class when the name starts with # 'inverted' class when the name starts with
# 'newkaku' # 'newkaku'
async def test_inverted_cover(hass: HomeAssistant, monkeypatch) -> None: async def test_inverted_cover(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Ensure states are restored on startup.""" """Ensure states are restored on startup."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},

View file

@ -31,7 +31,12 @@ from homeassistant.helpers import entity_registry as er
async def mock_rflink( async def mock_rflink(
hass, config, domain, monkeypatch, failures=None, failcommand=False hass: HomeAssistant,
config,
domain,
monkeypatch: pytest.MonkeyPatch,
failures=None,
failcommand=False,
): ):
"""Create mock RFLink asyncio protocol, test component setup.""" """Create mock RFLink asyncio protocol, test component setup."""
transport, protocol = (Mock(), Mock()) transport, protocol = (Mock(), Mock())
@ -77,7 +82,9 @@ async def mock_rflink(
return event_callback, mock_create, protocol, disconnect_callback return event_callback, mock_create, protocol, disconnect_callback
async def test_version_banner(hass: HomeAssistant, monkeypatch) -> None: async def test_version_banner(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test sending unknown commands doesn't cause issues.""" """Test sending unknown commands doesn't cause issues."""
# use sensor domain during testing main platform # use sensor domain during testing main platform
domain = "sensor" domain = "sensor"
@ -102,7 +109,9 @@ async def test_version_banner(hass: HomeAssistant, monkeypatch) -> None:
) )
async def test_send_no_wait(hass: HomeAssistant, monkeypatch) -> None: async def test_send_no_wait(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test command sending without ack.""" """Test command sending without ack."""
domain = "switch" domain = "switch"
config = { config = {
@ -126,7 +135,9 @@ async def test_send_no_wait(hass: HomeAssistant, monkeypatch) -> None:
assert protocol.send_command.call_args_list[0][0][1] == "off" assert protocol.send_command.call_args_list[0][0][1] == "off"
async def test_cover_send_no_wait(hass: HomeAssistant, monkeypatch) -> None: async def test_cover_send_no_wait(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test command sending to a cover device without ack.""" """Test command sending to a cover device without ack."""
domain = "cover" domain = "cover"
config = { config = {
@ -150,7 +161,9 @@ async def test_cover_send_no_wait(hass: HomeAssistant, monkeypatch) -> None:
assert protocol.send_command.call_args_list[0][0][1] == "STOP" assert protocol.send_command.call_args_list[0][0][1] == "STOP"
async def test_send_command(hass: HomeAssistant, monkeypatch) -> None: async def test_send_command(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test send_command service.""" """Test send_command service."""
domain = "rflink" domain = "rflink"
config = {"rflink": {"port": "/dev/ttyABC0"}} config = {"rflink": {"port": "/dev/ttyABC0"}}
@ -168,7 +181,9 @@ async def test_send_command(hass: HomeAssistant, monkeypatch) -> None:
assert protocol.send_command_ack.call_args_list[0][0][1] == "on" assert protocol.send_command_ack.call_args_list[0][0][1] == "on"
async def test_send_command_invalid_arguments(hass: HomeAssistant, monkeypatch) -> None: async def test_send_command_invalid_arguments(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test send_command service.""" """Test send_command service."""
domain = "rflink" domain = "rflink"
config = {"rflink": {"port": "/dev/ttyABC0"}} config = {"rflink": {"port": "/dev/ttyABC0"}}
@ -201,7 +216,9 @@ async def test_send_command_invalid_arguments(hass: HomeAssistant, monkeypatch)
assert not success, "send command should not succeed for unknown command" assert not success, "send command should not succeed for unknown command"
async def test_send_command_event_propagation(hass: HomeAssistant, monkeypatch) -> None: async def test_send_command_event_propagation(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test event propagation for send_command service.""" """Test event propagation for send_command service."""
domain = "light" domain = "light"
config = { config = {
@ -243,7 +260,9 @@ async def test_send_command_event_propagation(hass: HomeAssistant, monkeypatch)
assert hass.states.get(f"{domain}.test1").state == "off" assert hass.states.get(f"{domain}.test1").state == "off"
async def test_reconnecting_after_disconnect(hass: HomeAssistant, monkeypatch) -> None: async def test_reconnecting_after_disconnect(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""An unexpected disconnect should cause a reconnect.""" """An unexpected disconnect should cause a reconnect."""
domain = "sensor" domain = "sensor"
config = { config = {
@ -267,7 +286,9 @@ async def test_reconnecting_after_disconnect(hass: HomeAssistant, monkeypatch) -
assert mock_create.call_count == 2 assert mock_create.call_count == 2
async def test_reconnecting_after_failure(hass: HomeAssistant, monkeypatch) -> None: async def test_reconnecting_after_failure(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A failure to reconnect should be retried.""" """A failure to reconnect should be retried."""
domain = "sensor" domain = "sensor"
config = { config = {
@ -294,7 +315,9 @@ async def test_reconnecting_after_failure(hass: HomeAssistant, monkeypatch) -> N
assert mock_create.call_count == 3 assert mock_create.call_count == 3
async def test_error_when_not_connected(hass: HomeAssistant, monkeypatch) -> None: async def test_error_when_not_connected(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Sending command should error when not connected.""" """Sending command should error when not connected."""
domain = "switch" domain = "switch"
config = { config = {
@ -324,7 +347,9 @@ async def test_error_when_not_connected(hass: HomeAssistant, monkeypatch) -> Non
assert not success, "changing state should not succeed when disconnected" assert not success, "changing state should not succeed when disconnected"
async def test_async_send_command_error(hass: HomeAssistant, monkeypatch) -> None: async def test_async_send_command_error(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Sending command should error when protocol fails.""" """Sending command should error when protocol fails."""
domain = "rflink" domain = "rflink"
config = {"rflink": {"port": "/dev/ttyABC0"}} config = {"rflink": {"port": "/dev/ttyABC0"}}
@ -345,7 +370,9 @@ async def test_async_send_command_error(hass: HomeAssistant, monkeypatch) -> Non
assert protocol.send_command_ack.call_args_list[0][0][1] == SERVICE_TURN_OFF assert protocol.send_command_ack.call_args_list[0][0][1] == SERVICE_TURN_OFF
async def test_race_condition(hass: HomeAssistant, monkeypatch) -> None: async def test_race_condition(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test race condition for unknown components.""" """Test race condition for unknown components."""
domain = "light" domain = "light"
config = {"rflink": {"port": "/dev/ttyABC0"}, domain: {"platform": "rflink"}} config = {"rflink": {"port": "/dev/ttyABC0"}, domain: {"platform": "rflink"}}
@ -381,7 +408,7 @@ async def test_race_condition(hass: HomeAssistant, monkeypatch) -> None:
assert new_sensor.state == "on" assert new_sensor.state == "on"
async def test_not_connected(hass: HomeAssistant, monkeypatch) -> None: async def test_not_connected() -> None:
"""Test Error when sending commands to a disconnected device.""" """Test Error when sending commands to a disconnected device."""
test_device = RflinkCommand("DUMMY_DEVICE") test_device = RflinkCommand("DUMMY_DEVICE")
RflinkCommand.set_rflink_protocol(None) RflinkCommand.set_rflink_protocol(None)
@ -390,7 +417,9 @@ async def test_not_connected(hass: HomeAssistant, monkeypatch) -> None:
async def test_keepalive( async def test_keepalive(
hass: HomeAssistant, monkeypatch, caplog: pytest.LogCaptureFixture hass: HomeAssistant,
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Validate negative keepalive values.""" """Validate negative keepalive values."""
keepalive_value = -3 keepalive_value = -3
@ -418,7 +447,9 @@ async def test_keepalive(
async def test_keepalive_2( async def test_keepalive_2(
hass: HomeAssistant, monkeypatch, caplog: pytest.LogCaptureFixture hass: HomeAssistant,
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Validate very short keepalive values.""" """Validate very short keepalive values."""
keepalive_value = 30 keepalive_value = 30
@ -446,7 +477,9 @@ async def test_keepalive_2(
async def test_keepalive_3( async def test_keepalive_3(
hass: HomeAssistant, monkeypatch, caplog: pytest.LogCaptureFixture hass: HomeAssistant,
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Validate keepalive=0 value.""" """Validate keepalive=0 value."""
domain = RFLINK_DOMAIN domain = RFLINK_DOMAIN
@ -466,7 +499,9 @@ async def test_keepalive_3(
async def test_default_keepalive( async def test_default_keepalive(
hass: HomeAssistant, monkeypatch, caplog: pytest.LogCaptureFixture hass: HomeAssistant,
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Validate keepalive=0 value.""" """Validate keepalive=0 value."""
domain = RFLINK_DOMAIN domain = RFLINK_DOMAIN
@ -485,7 +520,9 @@ async def test_default_keepalive(
async def test_unique_id( async def test_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry, monkeypatch hass: HomeAssistant,
entity_registry: er.EntityRegistry,
monkeypatch: pytest.MonkeyPatch,
) -> None: ) -> None:
"""Validate the device unique_id.""" """Validate the device unique_id."""

View file

@ -5,6 +5,8 @@ control of RFLink switch devices.
""" """
import pytest
from homeassistant.components.light import ATTR_BRIGHTNESS from homeassistant.components.light import ATTR_BRIGHTNESS
from homeassistant.components.rflink import EVENT_BUTTON_PRESSED from homeassistant.components.rflink import EVENT_BUTTON_PRESSED
from homeassistant.const import ( from homeassistant.const import (
@ -38,7 +40,9 @@ CONFIG = {
} }
async def test_default_setup(hass: HomeAssistant, monkeypatch) -> None: async def test_default_setup(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test all basic functionality of the RFLink switch component.""" """Test all basic functionality of the RFLink switch component."""
# setup mocking rflink module # setup mocking rflink module
event_callback, create, protocol, _ = await mock_rflink( event_callback, create, protocol, _ = await mock_rflink(
@ -146,7 +150,9 @@ async def test_default_setup(hass: HomeAssistant, monkeypatch) -> None:
assert protocol.send_command_ack.call_args_list[5][0][1] == "7" assert protocol.send_command_ack.call_args_list[5][0][1] == "7"
async def test_firing_bus_event(hass: HomeAssistant, monkeypatch) -> None: async def test_firing_bus_event(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Incoming RFLink command events should be put on the HA event bus.""" """Incoming RFLink command events should be put on the HA event bus."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -181,7 +187,9 @@ async def test_firing_bus_event(hass: HomeAssistant, monkeypatch) -> None:
assert calls[0].data == {"state": "off", "entity_id": f"{DOMAIN}.test"} assert calls[0].data == {"state": "off", "entity_id": f"{DOMAIN}.test"}
async def test_signal_repetitions(hass: HomeAssistant, monkeypatch) -> None: async def test_signal_repetitions(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Command should be sent amount of configured repetitions.""" """Command should be sent amount of configured repetitions."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -237,7 +245,9 @@ async def test_signal_repetitions(hass: HomeAssistant, monkeypatch) -> None:
assert protocol.send_command_ack.call_count == 8 assert protocol.send_command_ack.call_count == 8
async def test_signal_repetitions_alternation(hass: HomeAssistant, monkeypatch) -> None: async def test_signal_repetitions_alternation(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Simultaneously switching entities must alternate repetitions.""" """Simultaneously switching entities must alternate repetitions."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -268,7 +278,9 @@ async def test_signal_repetitions_alternation(hass: HomeAssistant, monkeypatch)
assert protocol.send_command_ack.call_args_list[3][0][0] == "protocol_0_1" assert protocol.send_command_ack.call_args_list[3][0][0] == "protocol_0_1"
async def test_signal_repetitions_cancelling(hass: HomeAssistant, monkeypatch) -> None: async def test_signal_repetitions_cancelling(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Cancel outstanding repetitions when state changed.""" """Cancel outstanding repetitions when state changed."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -302,7 +314,9 @@ async def test_signal_repetitions_cancelling(hass: HomeAssistant, monkeypatch) -
] ]
async def test_type_toggle(hass: HomeAssistant, monkeypatch) -> None: async def test_type_toggle(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test toggle type lights (on/on).""" """Test toggle type lights (on/on)."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -347,7 +361,9 @@ async def test_type_toggle(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get(f"{DOMAIN}.toggle_test").state == "off" assert hass.states.get(f"{DOMAIN}.toggle_test").state == "off"
async def test_set_level_command(hass: HomeAssistant, monkeypatch) -> None: async def test_set_level_command(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test 'set_level=XX' events.""" """Test 'set_level=XX' events."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -434,7 +450,9 @@ async def test_set_level_command(hass: HomeAssistant, monkeypatch) -> None:
assert state.attributes[ATTR_BRIGHTNESS] == 0 assert state.attributes[ATTR_BRIGHTNESS] == 0
async def test_group_alias(hass: HomeAssistant, monkeypatch) -> None: async def test_group_alias(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Group aliases should only respond to group commands (allon/alloff).""" """Group aliases should only respond to group commands (allon/alloff)."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -471,7 +489,9 @@ async def test_group_alias(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get(f"{DOMAIN}.test2").state == "on" assert hass.states.get(f"{DOMAIN}.test2").state == "on"
async def test_nogroup_alias(hass: HomeAssistant, monkeypatch) -> None: async def test_nogroup_alias(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Non group aliases should not respond to group commands.""" """Non group aliases should not respond to group commands."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -504,7 +524,9 @@ async def test_nogroup_alias(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get(f"{DOMAIN}.test").state == "on" assert hass.states.get(f"{DOMAIN}.test").state == "on"
async def test_nogroup_device_id(hass: HomeAssistant, monkeypatch) -> None: async def test_nogroup_device_id(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Device id that do not respond to group commands (allon/alloff).""" """Device id that do not respond to group commands (allon/alloff)."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -532,7 +554,9 @@ async def test_nogroup_device_id(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get(f"{DOMAIN}.test").state == "on" assert hass.states.get(f"{DOMAIN}.test").state == "on"
async def test_disable_automatic_add(hass: HomeAssistant, monkeypatch) -> None: async def test_disable_automatic_add(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""If disabled new devices should not be automatically added.""" """If disabled new devices should not be automatically added."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -550,7 +574,9 @@ async def test_disable_automatic_add(hass: HomeAssistant, monkeypatch) -> None:
assert not hass.states.get(f"{DOMAIN}.protocol_0_0") assert not hass.states.get(f"{DOMAIN}.protocol_0_0")
async def test_restore_state(hass: HomeAssistant, monkeypatch) -> None: async def test_restore_state(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Ensure states are restored on startup.""" """Ensure states are restored on startup."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},

View file

@ -5,6 +5,8 @@ automatic sensor creation.
""" """
import pytest
from homeassistant.components.rflink import ( from homeassistant.components.rflink import (
CONF_RECONNECT_INTERVAL, CONF_RECONNECT_INTERVAL,
DATA_ENTITY_LOOKUP, DATA_ENTITY_LOOKUP,
@ -39,7 +41,9 @@ CONFIG = {
} }
async def test_default_setup(hass: HomeAssistant, monkeypatch) -> None: async def test_default_setup(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test all basic functionality of the rflink sensor component.""" """Test all basic functionality of the rflink sensor component."""
# setup mocking rflink module # setup mocking rflink module
event_callback, create, _, _ = await mock_rflink(hass, CONFIG, DOMAIN, monkeypatch) event_callback, create, _, _ = await mock_rflink(hass, CONFIG, DOMAIN, monkeypatch)
@ -100,7 +104,9 @@ async def test_default_setup(hass: HomeAssistant, monkeypatch) -> None:
assert bat_sensor.attributes[ATTR_ICON] == "mdi:battery" assert bat_sensor.attributes[ATTR_ICON] == "mdi:battery"
async def test_disable_automatic_add(hass: HomeAssistant, monkeypatch) -> None: async def test_disable_automatic_add(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""If disabled new devices should not be automatically added.""" """If disabled new devices should not be automatically added."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -125,7 +131,9 @@ async def test_disable_automatic_add(hass: HomeAssistant, monkeypatch) -> None:
assert not hass.states.get("sensor.test2") assert not hass.states.get("sensor.test2")
async def test_entity_availability(hass: HomeAssistant, monkeypatch) -> None: async def test_entity_availability(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""If Rflink device is disconnected, entities should become unavailable.""" """If Rflink device is disconnected, entities should become unavailable."""
# Make sure Rflink mock does not 'recover' to quickly from the # Make sure Rflink mock does not 'recover' to quickly from the
# disconnect or else the unavailability cannot be measured # disconnect or else the unavailability cannot be measured
@ -160,7 +168,7 @@ async def test_entity_availability(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get("sensor.test").state == STATE_UNKNOWN assert hass.states.get("sensor.test").state == STATE_UNKNOWN
async def test_aliases(hass: HomeAssistant, monkeypatch) -> None: async def test_aliases(hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch) -> None:
"""Validate the response to sensor's alias (with aliases).""" """Validate the response to sensor's alias (with aliases)."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -202,7 +210,9 @@ async def test_aliases(hass: HomeAssistant, monkeypatch) -> None:
assert updated_sensor.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE assert updated_sensor.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE
async def test_race_condition(hass: HomeAssistant, monkeypatch) -> None: async def test_race_condition(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test race condition for unknown components.""" """Test race condition for unknown components."""
config = {"rflink": {"port": "/dev/ttyABC0"}, DOMAIN: {"platform": "rflink"}} config = {"rflink": {"port": "/dev/ttyABC0"}, DOMAIN: {"platform": "rflink"}}
tmp_entity = TMP_ENTITY.format("test3") tmp_entity = TMP_ENTITY.format("test3")
@ -241,7 +251,9 @@ async def test_race_condition(hass: HomeAssistant, monkeypatch) -> None:
assert new_sensor.state == "ko" assert new_sensor.state == "ko"
async def test_sensor_attributes(hass: HomeAssistant, monkeypatch) -> None: async def test_sensor_attributes(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Validate the sensor attributes.""" """Validate the sensor attributes."""
config = { config = {

View file

@ -5,6 +5,8 @@ control of Rflink switch devices.
""" """
import pytest
from homeassistant.components.rflink import EVENT_BUTTON_PRESSED from homeassistant.components.rflink import EVENT_BUTTON_PRESSED
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
@ -33,7 +35,9 @@ CONFIG = {
} }
async def test_default_setup(hass: HomeAssistant, monkeypatch) -> None: async def test_default_setup(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Test all basic functionality of the rflink switch component.""" """Test all basic functionality of the rflink switch component."""
# setup mocking rflink module # setup mocking rflink module
event_callback, create, protocol, _ = await mock_rflink( event_callback, create, protocol, _ = await mock_rflink(
@ -93,7 +97,9 @@ async def test_default_setup(hass: HomeAssistant, monkeypatch) -> None:
assert protocol.send_command_ack.call_args_list[1][0][1] == "on" assert protocol.send_command_ack.call_args_list[1][0][1] == "on"
async def test_group_alias(hass: HomeAssistant, monkeypatch) -> None: async def test_group_alias(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Group aliases should only respond to group commands (allon/alloff).""" """Group aliases should only respond to group commands (allon/alloff)."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -123,7 +129,9 @@ async def test_group_alias(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get(f"{DOMAIN}.test").state == "on" assert hass.states.get(f"{DOMAIN}.test").state == "on"
async def test_nogroup_alias(hass: HomeAssistant, monkeypatch) -> None: async def test_nogroup_alias(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Non group aliases should not respond to group commands.""" """Non group aliases should not respond to group commands."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -156,7 +164,9 @@ async def test_nogroup_alias(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get(f"{DOMAIN}.test").state == "on" assert hass.states.get(f"{DOMAIN}.test").state == "on"
async def test_nogroup_device_id(hass: HomeAssistant, monkeypatch) -> None: async def test_nogroup_device_id(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Device id that do not respond to group commands (allon/alloff).""" """Device id that do not respond to group commands (allon/alloff)."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -184,7 +194,9 @@ async def test_nogroup_device_id(hass: HomeAssistant, monkeypatch) -> None:
assert hass.states.get(f"{DOMAIN}.test").state == "on" assert hass.states.get(f"{DOMAIN}.test").state == "on"
async def test_device_defaults(hass: HomeAssistant, monkeypatch) -> None: async def test_device_defaults(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Event should fire if device_defaults config says so.""" """Event should fire if device_defaults config says so."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -216,7 +228,9 @@ async def test_device_defaults(hass: HomeAssistant, monkeypatch) -> None:
assert calls[0].data == {"state": "off", "entity_id": f"{DOMAIN}.test"} assert calls[0].data == {"state": "off", "entity_id": f"{DOMAIN}.test"}
async def test_not_firing_default(hass: HomeAssistant, monkeypatch) -> None: async def test_not_firing_default(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""By default no bus events should be fired.""" """By default no bus events should be fired."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},
@ -246,7 +260,9 @@ async def test_not_firing_default(hass: HomeAssistant, monkeypatch) -> None:
assert not calls, "an event has been fired" assert not calls, "an event has been fired"
async def test_restore_state(hass: HomeAssistant, monkeypatch) -> None: async def test_restore_state(
hass: HomeAssistant, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Ensure states are restored on startup.""" """Ensure states are restored on startup."""
config = { config = {
"rflink": {"port": "/dev/ttyABC0"}, "rflink": {"port": "/dev/ttyABC0"},

View file

@ -4,10 +4,9 @@ from homeassistant.components.rflink.utils import (
brightness_to_rflink, brightness_to_rflink,
rflink_to_brightness, rflink_to_brightness,
) )
from homeassistant.core import HomeAssistant
async def test_utils(hass: HomeAssistant, monkeypatch) -> None: async def test_utils() -> None:
"""Test all utils methods.""" """Test all utils methods."""
# test brightness_to_rflink # test brightness_to_rflink
assert brightness_to_rflink(0) == 0 assert brightness_to_rflink(0) == 0