Fix homekit_controller tests to avoid global aid generation (#119852)

This commit is contained in:
J. Nick Koston
2024-07-17 19:10:02 -05:00
committed by GitHub
parent 454ca0ce95
commit e2276458ed
25 changed files with 792 additions and 344 deletions

View File

@@ -1,5 +1,7 @@
"""Basic checks for HomeKitSwitch."""
from collections.abc import Callable
from aiohomekit.model.characteristics import (
CharacteristicsTypes,
InUseValues,
@@ -10,7 +12,7 @@ from aiohomekit.model.services import ServicesTypes
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from .common import get_next_aid, setup_test_component
from .common import setup_test_component
def create_switch_service(accessory):
@@ -50,9 +52,11 @@ def create_char_switch_service(accessory):
on_char.value = False
async def test_switch_change_outlet_state(hass: HomeAssistant) -> None:
async def test_switch_change_outlet_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a HomeKit outlet on and off again."""
helper = await setup_test_component(hass, create_switch_service)
helper = await setup_test_component(hass, get_next_aid(), create_switch_service)
await hass.services.async_call(
"switch", "turn_on", {"entity_id": "switch.testdevice"}, blocking=True
@@ -75,9 +79,11 @@ async def test_switch_change_outlet_state(hass: HomeAssistant) -> None:
)
async def test_switch_read_outlet_state(hass: HomeAssistant) -> None:
async def test_switch_read_outlet_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit outlet accessory."""
helper = await setup_test_component(hass, create_switch_service)
helper = await setup_test_component(hass, get_next_aid(), create_switch_service)
# Initial state is that the switch is off and the outlet isn't in use
switch_1 = await helper.poll_and_get_state()
@@ -108,9 +114,11 @@ async def test_switch_read_outlet_state(hass: HomeAssistant) -> None:
assert switch_1.attributes["outlet_in_use"] is True
async def test_valve_change_active_state(hass: HomeAssistant) -> None:
async def test_valve_change_active_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a valve on and off again."""
helper = await setup_test_component(hass, create_valve_service)
helper = await setup_test_component(hass, get_next_aid(), create_valve_service)
await hass.services.async_call(
"switch", "turn_on", {"entity_id": "switch.testdevice"}, blocking=True
@@ -133,9 +141,11 @@ async def test_valve_change_active_state(hass: HomeAssistant) -> None:
)
async def test_valve_read_state(hass: HomeAssistant) -> None:
async def test_valve_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a valve accessory."""
helper = await setup_test_component(hass, create_valve_service)
helper = await setup_test_component(hass, get_next_aid(), create_valve_service)
# Initial state is that the switch is off and the outlet isn't in use
switch_1 = await helper.poll_and_get_state()
@@ -166,10 +176,12 @@ async def test_valve_read_state(hass: HomeAssistant) -> None:
assert switch_1.attributes["in_use"] is False
async def test_char_switch_change_state(hass: HomeAssistant) -> None:
async def test_char_switch_change_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a characteristic on and off again."""
helper = await setup_test_component(
hass, create_char_switch_service, suffix="pairing_mode"
hass, get_next_aid(), create_char_switch_service, suffix="pairing_mode"
)
await hass.services.async_call(
@@ -199,10 +211,12 @@ async def test_char_switch_change_state(hass: HomeAssistant) -> None:
)
async def test_char_switch_read_state(hass: HomeAssistant) -> None:
async def test_char_switch_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit characteristic switch."""
helper = await setup_test_component(
hass, create_char_switch_service, suffix="pairing_mode"
hass, get_next_aid(), create_char_switch_service, suffix="pairing_mode"
)
# Simulate that someone switched on the device in the real world not via HA
@@ -221,7 +235,9 @@ async def test_char_switch_read_state(hass: HomeAssistant) -> None:
async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None:
"""Test a we can migrate a switch unique id."""
aid = get_next_aid()
@@ -235,7 +251,9 @@ async def test_migrate_unique_id(
"homekit_controller",
f"homekit-0001-aid:{aid}-sid:8-cid:9",
)
await setup_test_component(hass, create_char_switch_service, suffix="pairing_mode")
await setup_test_component(
hass, aid, create_char_switch_service, suffix="pairing_mode"
)
assert (
entity_registry.async_get(switch_entry.entity_id).unique_id