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
from aiohomekit.model.services import ServicesTypes
@@ -13,7 +15,7 @@ from homeassistant.const import ATTR_SUPPORTED_FEATURES, STATE_UNAVAILABLE
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
LIGHT_BULB_NAME = "TestDevice"
LIGHT_BULB_ENTITY_ID = "light.testdevice"
@@ -55,9 +57,13 @@ def create_lightbulb_service_with_color_temp(accessory):
return service
async def test_switch_change_light_state(hass: HomeAssistant) -> None:
async def test_switch_change_light_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a HomeKit light on and off again."""
helper = await setup_test_component(hass, create_lightbulb_service_with_hs)
helper = await setup_test_component(
hass, get_next_aid(), create_lightbulb_service_with_hs
)
await hass.services.async_call(
"light",
@@ -102,9 +108,13 @@ async def test_switch_change_light_state(hass: HomeAssistant) -> None:
)
async def test_switch_change_light_state_color_temp(hass: HomeAssistant) -> None:
async def test_switch_change_light_state_color_temp(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn change color_temp."""
helper = await setup_test_component(hass, create_lightbulb_service_with_color_temp)
helper = await setup_test_component(
hass, get_next_aid(), create_lightbulb_service_with_color_temp
)
await hass.services.async_call(
"light",
@@ -122,9 +132,11 @@ async def test_switch_change_light_state_color_temp(hass: HomeAssistant) -> None
)
async def test_switch_read_light_state_dimmer(hass: HomeAssistant) -> None:
async def test_switch_read_light_state_dimmer(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit light accessory."""
helper = await setup_test_component(hass, create_lightbulb_service)
helper = await setup_test_component(hass, get_next_aid(), create_lightbulb_service)
# Initial state is that the light is off
state = await helper.poll_and_get_state()
@@ -157,9 +169,11 @@ async def test_switch_read_light_state_dimmer(hass: HomeAssistant) -> None:
assert state.state == "off"
async def test_switch_push_light_state_dimmer(hass: HomeAssistant) -> None:
async def test_switch_push_light_state_dimmer(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit light accessory."""
helper = await setup_test_component(hass, create_lightbulb_service)
helper = await setup_test_component(hass, get_next_aid(), create_lightbulb_service)
# Initial state is that the light is off
state = hass.states.get(LIGHT_BULB_ENTITY_ID)
@@ -185,9 +199,13 @@ async def test_switch_push_light_state_dimmer(hass: HomeAssistant) -> None:
assert state.state == "off"
async def test_switch_read_light_state_hs(hass: HomeAssistant) -> None:
async def test_switch_read_light_state_hs(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit light accessory."""
helper = await setup_test_component(hass, create_lightbulb_service_with_hs)
helper = await setup_test_component(
hass, get_next_aid(), create_lightbulb_service_with_hs
)
# Initial state is that the light is off
state = await helper.poll_and_get_state()
@@ -248,9 +266,13 @@ async def test_switch_read_light_state_hs(hass: HomeAssistant) -> None:
assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0
async def test_switch_push_light_state_hs(hass: HomeAssistant) -> None:
async def test_switch_push_light_state_hs(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit light accessory."""
helper = await setup_test_component(hass, create_lightbulb_service_with_hs)
helper = await setup_test_component(
hass, get_next_aid(), create_lightbulb_service_with_hs
)
# Initial state is that the light is off
state = hass.states.get(LIGHT_BULB_ENTITY_ID)
@@ -279,9 +301,13 @@ async def test_switch_push_light_state_hs(hass: HomeAssistant) -> None:
assert state.state == "off"
async def test_switch_read_light_state_color_temp(hass: HomeAssistant) -> None:
async def test_switch_read_light_state_color_temp(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the color_temp of a light accessory."""
helper = await setup_test_component(hass, create_lightbulb_service_with_color_temp)
helper = await setup_test_component(
hass, get_next_aid(), create_lightbulb_service_with_color_temp
)
# Initial state is that the light is off
state = await helper.poll_and_get_state()
@@ -307,9 +333,13 @@ async def test_switch_read_light_state_color_temp(hass: HomeAssistant) -> None:
assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0
async def test_switch_push_light_state_color_temp(hass: HomeAssistant) -> None:
async def test_switch_push_light_state_color_temp(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit light accessory."""
helper = await setup_test_component(hass, create_lightbulb_service_with_color_temp)
helper = await setup_test_component(
hass, get_next_aid(), create_lightbulb_service_with_color_temp
)
# Initial state is that the light is off
state = hass.states.get(LIGHT_BULB_ENTITY_ID)
@@ -328,9 +358,13 @@ async def test_switch_push_light_state_color_temp(hass: HomeAssistant) -> None:
assert state.attributes["color_temp"] == 400
async def test_light_becomes_unavailable_but_recovers(hass: HomeAssistant) -> None:
async def test_light_becomes_unavailable_but_recovers(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test transition to and from unavailable state."""
helper = await setup_test_component(hass, create_lightbulb_service_with_color_temp)
helper = await setup_test_component(
hass, get_next_aid(), create_lightbulb_service_with_color_temp
)
# Initial state is that the light is off
state = await helper.poll_and_get_state()
@@ -356,9 +390,13 @@ async def test_light_becomes_unavailable_but_recovers(hass: HomeAssistant) -> No
assert state.attributes["color_temp"] == 400
async def test_light_unloaded_removed(hass: HomeAssistant) -> None:
async def test_light_unloaded_removed(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test entity and HKDevice are correctly unloaded and removed."""
helper = await setup_test_component(hass, create_lightbulb_service_with_color_temp)
helper = await setup_test_component(
hass, get_next_aid(), create_lightbulb_service_with_color_temp
)
# Initial state is that the light is off
state = await helper.poll_and_get_state()
@@ -382,7 +420,9 @@ async def test_light_unloaded_removed(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 light unique id."""
aid = get_next_aid()
@@ -391,7 +431,7 @@ async def test_migrate_unique_id(
"homekit_controller",
f"homekit-00:00:00:00:00:00-{aid}-8",
)
await setup_test_component(hass, create_lightbulb_service_with_color_temp)
await setup_test_component(hass, aid, create_lightbulb_service_with_color_temp)
assert (
entity_registry.async_get(light_entry.entity_id).unique_id
@@ -400,7 +440,9 @@ async def test_migrate_unique_id(
async def test_only_migrate_once(
hass: HomeAssistant, entity_registry: er.EntityRegistry
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None:
"""Test a we handle migration happening after an upgrade and than a downgrade and then an upgrade."""
aid = get_next_aid()
@@ -414,7 +456,7 @@ async def test_only_migrate_once(
"homekit_controller",
f"00:00:00:00:00:00_{aid}_8",
)
await setup_test_component(hass, create_lightbulb_service_with_color_temp)
await setup_test_component(hass, aid, create_lightbulb_service_with_color_temp)
assert (
entity_registry.async_get(old_light_entry.entity_id).unique_id