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
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 792 additions and 344 deletions

View file

@ -14,6 +14,6 @@
"documentation": "https://www.home-assistant.io/integrations/homekit_controller", "documentation": "https://www.home-assistant.io/integrations/homekit_controller",
"iot_class": "local_push", "iot_class": "local_push",
"loggers": ["aiohomekit", "commentjson"], "loggers": ["aiohomekit", "commentjson"],
"requirements": ["aiohomekit==3.1.5"], "requirements": ["aiohomekit==3.2.0"],
"zeroconf": ["_hap._tcp.local.", "_hap._udp.local."] "zeroconf": ["_hap._tcp.local.", "_hap._udp.local."]
} }

View file

@ -255,7 +255,7 @@ aioguardian==2022.07.0
aioharmony==0.2.10 aioharmony==0.2.10
# homeassistant.components.homekit_controller # homeassistant.components.homekit_controller
aiohomekit==3.1.5 aiohomekit==3.2.0
# homeassistant.components.hue # homeassistant.components.hue
aiohue==4.7.1 aiohue==4.7.1

View file

@ -237,7 +237,7 @@ aioguardian==2022.07.0
aioharmony==0.2.10 aioharmony==0.2.10
# homeassistant.components.homekit_controller # homeassistant.components.homekit_controller
aiohomekit==3.1.5 aiohomekit==3.2.0
# homeassistant.components.hue # homeassistant.components.hue
aiohue==4.7.1 aiohue==4.7.1

View file

@ -11,12 +11,7 @@ from unittest import mock
from aiohomekit.controller.abstract import AbstractDescription, AbstractPairing from aiohomekit.controller.abstract import AbstractDescription, AbstractPairing
from aiohomekit.hkjson import loads as hkloads from aiohomekit.hkjson import loads as hkloads
from aiohomekit.model import ( from aiohomekit.model import Accessories, AccessoriesState, Accessory
Accessories,
AccessoriesState,
Accessory,
mixin as model_mixin,
)
from aiohomekit.testing import FakeController, FakePairing from aiohomekit.testing import FakeController, FakePairing
from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.device_automation import DeviceAutomationType
@ -282,7 +277,7 @@ async def device_config_changed(hass: HomeAssistant, accessories: Accessories):
async def setup_test_component( async def setup_test_component(
hass, setup_accessory, capitalize=False, suffix=None, connection=None hass, aid, setup_accessory, capitalize=False, suffix=None, connection=None
): ):
"""Load a fake homekit accessory based on a homekit accessory model. """Load a fake homekit accessory based on a homekit accessory model.
@ -291,7 +286,7 @@ async def setup_test_component(
If suffix is set, entityId will include the suffix If suffix is set, entityId will include the suffix
""" """
accessory = Accessory.create_with_info( accessory = Accessory.create_with_info(
"TestDevice", "example.com", "Test", "0001", "0.1" aid, "TestDevice", "example.com", "Test", "0001", "0.1"
) )
setup_accessory(accessory) setup_accessory(accessory)
@ -397,8 +392,3 @@ async def assert_devices_and_entities_created(
# Root device must not have a via, otherwise its not the device # Root device must not have a via, otherwise its not the device
assert root_device.via_device_id is None assert root_device.via_device_id is None
def get_next_aid():
"""Get next aid."""
return model_mixin.id_counter + 1

View file

@ -1,6 +1,6 @@
"""HomeKit controller session fixtures.""" """HomeKit controller session fixtures."""
from collections.abc import Generator from collections.abc import Callable, Generator
import datetime import datetime
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
@ -44,3 +44,16 @@ def hk_mock_async_zeroconf(mock_async_zeroconf: MagicMock) -> None:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def auto_mock_bluetooth(mock_bluetooth: None) -> None: def auto_mock_bluetooth(mock_bluetooth: None) -> None:
"""Auto mock bluetooth.""" """Auto mock bluetooth."""
@pytest.fixture
def get_next_aid() -> Generator[Callable[[], int]]:
"""Generate a function that returns increasing accessory ids."""
id_counter = 0
def _get_id():
nonlocal id_counter
id_counter += 1
return id_counter
return _get_id

View file

@ -1,12 +1,14 @@
"""Basic checks for HomeKitalarm_control_panel.""" """Basic checks for HomeKitalarm_control_panel."""
from collections.abc import Callable
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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_security_system_service(accessory): def create_security_system_service(accessory):
@ -27,9 +29,13 @@ def create_security_system_service(accessory):
targ_state.value = 50 targ_state.value = 50
async def test_switch_change_alarm_state(hass: HomeAssistant) -> None: async def test_switch_change_alarm_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a HomeKit alarm on and off again.""" """Test that we can turn a HomeKit alarm on and off again."""
helper = await setup_test_component(hass, create_security_system_service) helper = await setup_test_component(
hass, get_next_aid(), create_security_system_service
)
await hass.services.async_call( await hass.services.async_call(
"alarm_control_panel", "alarm_control_panel",
@ -84,9 +90,13 @@ async def test_switch_change_alarm_state(hass: HomeAssistant) -> None:
) )
async def test_switch_read_alarm_state(hass: HomeAssistant) -> None: async def test_switch_read_alarm_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit alarm accessory.""" """Test that we can read the state of a HomeKit alarm accessory."""
helper = await setup_test_component(hass, create_security_system_service) helper = await setup_test_component(
hass, get_next_aid(), create_security_system_service
)
await helper.async_update( await helper.async_update(
ServicesTypes.SECURITY_SYSTEM, ServicesTypes.SECURITY_SYSTEM,
@ -126,7 +136,9 @@ async def test_switch_read_alarm_state(hass: HomeAssistant) -> None:
async def test_migrate_unique_id( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we can migrate a alarm_control_panel unique id.""" """Test a we can migrate a alarm_control_panel unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -135,7 +147,7 @@ async def test_migrate_unique_id(
"homekit_controller", "homekit_controller",
f"homekit-00:00:00:00:00:00-{aid}-8", f"homekit-00:00:00:00:00:00-{aid}-8",
) )
await setup_test_component(hass, create_security_system_service) await setup_test_component(hass, aid, create_security_system_service)
assert ( assert (
entity_registry.async_get(alarm_control_panel_entry.entity_id).unique_id entity_registry.async_get(alarm_control_panel_entry.entity_id).unique_id

View file

@ -1,5 +1,7 @@
"""Basic checks for HomeKit motion sensors and contact sensors.""" """Basic checks for HomeKit motion sensors and contact sensors."""
from collections.abc import Callable
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
@ -7,7 +9,7 @@ from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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_motion_sensor_service(accessory): def create_motion_sensor_service(accessory):
@ -18,9 +20,13 @@ def create_motion_sensor_service(accessory):
cur_state.value = 0 cur_state.value = 0
async def test_motion_sensor_read_state(hass: HomeAssistant) -> None: async def test_motion_sensor_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit motion sensor accessory.""" """Test that we can read the state of a HomeKit motion sensor accessory."""
helper = await setup_test_component(hass, create_motion_sensor_service) helper = await setup_test_component(
hass, get_next_aid(), create_motion_sensor_service
)
await helper.async_update( await helper.async_update(
ServicesTypes.MOTION_SENSOR, {CharacteristicsTypes.MOTION_DETECTED: False} ServicesTypes.MOTION_SENSOR, {CharacteristicsTypes.MOTION_DETECTED: False}
@ -45,9 +51,13 @@ def create_contact_sensor_service(accessory):
cur_state.value = 0 cur_state.value = 0
async def test_contact_sensor_read_state(hass: HomeAssistant) -> None: async def test_contact_sensor_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit contact accessory.""" """Test that we can read the state of a HomeKit contact accessory."""
helper = await setup_test_component(hass, create_contact_sensor_service) helper = await setup_test_component(
hass, get_next_aid(), create_contact_sensor_service
)
await helper.async_update( await helper.async_update(
ServicesTypes.CONTACT_SENSOR, {CharacteristicsTypes.CONTACT_STATE: 0} ServicesTypes.CONTACT_SENSOR, {CharacteristicsTypes.CONTACT_STATE: 0}
@ -72,9 +82,13 @@ def create_smoke_sensor_service(accessory):
cur_state.value = 0 cur_state.value = 0
async def test_smoke_sensor_read_state(hass: HomeAssistant) -> None: async def test_smoke_sensor_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit contact accessory.""" """Test that we can read the state of a HomeKit contact accessory."""
helper = await setup_test_component(hass, create_smoke_sensor_service) helper = await setup_test_component(
hass, get_next_aid(), create_smoke_sensor_service
)
await helper.async_update( await helper.async_update(
ServicesTypes.SMOKE_SENSOR, {CharacteristicsTypes.SMOKE_DETECTED: 0} ServicesTypes.SMOKE_SENSOR, {CharacteristicsTypes.SMOKE_DETECTED: 0}
@ -99,9 +113,13 @@ def create_carbon_monoxide_sensor_service(accessory):
cur_state.value = 0 cur_state.value = 0
async def test_carbon_monoxide_sensor_read_state(hass: HomeAssistant) -> None: async def test_carbon_monoxide_sensor_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit contact accessory.""" """Test that we can read the state of a HomeKit contact accessory."""
helper = await setup_test_component(hass, create_carbon_monoxide_sensor_service) helper = await setup_test_component(
hass, get_next_aid(), create_carbon_monoxide_sensor_service
)
await helper.async_update( await helper.async_update(
ServicesTypes.CARBON_MONOXIDE_SENSOR, ServicesTypes.CARBON_MONOXIDE_SENSOR,
@ -128,9 +146,13 @@ def create_occupancy_sensor_service(accessory):
cur_state.value = 0 cur_state.value = 0
async def test_occupancy_sensor_read_state(hass: HomeAssistant) -> None: async def test_occupancy_sensor_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit occupancy sensor accessory.""" """Test that we can read the state of a HomeKit occupancy sensor accessory."""
helper = await setup_test_component(hass, create_occupancy_sensor_service) helper = await setup_test_component(
hass, get_next_aid(), create_occupancy_sensor_service
)
await helper.async_update( await helper.async_update(
ServicesTypes.OCCUPANCY_SENSOR, {CharacteristicsTypes.OCCUPANCY_DETECTED: False} ServicesTypes.OCCUPANCY_SENSOR, {CharacteristicsTypes.OCCUPANCY_DETECTED: False}
@ -155,9 +177,13 @@ def create_leak_sensor_service(accessory):
cur_state.value = 0 cur_state.value = 0
async def test_leak_sensor_read_state(hass: HomeAssistant) -> None: async def test_leak_sensor_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit leak sensor accessory.""" """Test that we can read the state of a HomeKit leak sensor accessory."""
helper = await setup_test_component(hass, create_leak_sensor_service) helper = await setup_test_component(
hass, get_next_aid(), create_leak_sensor_service
)
await helper.async_update( await helper.async_update(
ServicesTypes.LEAK_SENSOR, {CharacteristicsTypes.LEAK_DETECTED: 0} ServicesTypes.LEAK_SENSOR, {CharacteristicsTypes.LEAK_DETECTED: 0}
@ -175,7 +201,9 @@ async def test_leak_sensor_read_state(hass: HomeAssistant) -> None:
async def test_migrate_unique_id( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we can migrate a binary_sensor unique id.""" """Test a we can migrate a binary_sensor unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -184,7 +212,7 @@ async def test_migrate_unique_id(
"homekit_controller", "homekit_controller",
f"homekit-00:00:00:00:00:00-{aid}-8", f"homekit-00:00:00:00:00:00-{aid}-8",
) )
await setup_test_component(hass, create_leak_sensor_service) await setup_test_component(hass, aid, create_leak_sensor_service)
assert ( assert (
entity_registry.async_get(binary_sensor_entry.entity_id).unique_id entity_registry.async_get(binary_sensor_entry.entity_id).unique_id

View file

@ -1,12 +1,14 @@
"""Basic checks for HomeKit button.""" """Basic checks for HomeKit button."""
from collections.abc import Callable
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .common import Helper, get_next_aid, setup_test_component from .common import Helper, setup_test_component
def create_switch_with_setup_button(accessory): def create_switch_with_setup_button(accessory):
@ -39,9 +41,13 @@ def create_switch_with_ecobee_clear_hold_button(accessory):
return service return service
async def test_press_button(hass: HomeAssistant) -> None: async def test_press_button(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test a switch service that has a button characteristic is correctly handled.""" """Test a switch service that has a button characteristic is correctly handled."""
helper = await setup_test_component(hass, create_switch_with_setup_button) helper = await setup_test_component(
hass, get_next_aid(), create_switch_with_setup_button
)
# Helper will be for the primary entity, which is the outlet. Make a helper for the button. # Helper will be for the primary entity, which is the outlet. Make a helper for the button.
button = Helper( button = Helper(
@ -66,10 +72,12 @@ async def test_press_button(hass: HomeAssistant) -> None:
) )
async def test_ecobee_clear_hold_press_button(hass: HomeAssistant) -> None: async def test_ecobee_clear_hold_press_button(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test ecobee clear hold button characteristic is correctly handled.""" """Test ecobee clear hold button characteristic is correctly handled."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_switch_with_ecobee_clear_hold_button hass, get_next_aid(), create_switch_with_ecobee_clear_hold_button
) )
# Helper will be for the primary entity, which is the outlet. Make a helper for the button. # Helper will be for the primary entity, which is the outlet. Make a helper for the button.
@ -96,7 +104,9 @@ async def test_ecobee_clear_hold_press_button(hass: HomeAssistant) -> None:
async def test_migrate_unique_id( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we can migrate a button unique id.""" """Test a we can migrate a button unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -105,7 +115,7 @@ async def test_migrate_unique_id(
"homekit_controller", "homekit_controller",
f"homekit-0001-aid:{aid}-sid:1-cid:2", f"homekit-0001-aid:{aid}-sid:1-cid:2",
) )
await setup_test_component(hass, create_switch_with_ecobee_clear_hold_button) await setup_test_component(hass, aid, create_switch_with_ecobee_clear_hold_button)
assert ( assert (
entity_registry.async_get(button_entry.entity_id).unique_id entity_registry.async_get(button_entry.entity_id).unique_id
== f"00:00:00:00:00:00_{aid}_1_2" == f"00:00:00:00:00:00_{aid}_1_2"

View file

@ -1,6 +1,7 @@
"""Basic checks for HomeKit cameras.""" """Basic checks for HomeKit cameras."""
import base64 import base64
from collections.abc import Callable
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
from aiohomekit.testing import FAKE_CAMERA_IMAGE from aiohomekit.testing import FAKE_CAMERA_IMAGE
@ -9,7 +10,7 @@ from homeassistant.components import camera
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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_camera(accessory): def create_camera(accessory):
@ -18,7 +19,9 @@ def create_camera(accessory):
async def test_migrate_unique_ids( async def test_migrate_unique_ids(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test migrating entity unique ids.""" """Test migrating entity unique ids."""
aid = get_next_aid() aid = get_next_aid()
@ -27,23 +30,23 @@ async def test_migrate_unique_ids(
"homekit_controller", "homekit_controller",
f"homekit-0001-aid:{aid}", f"homekit-0001-aid:{aid}",
) )
await setup_test_component(hass, create_camera) await setup_test_component(hass, aid, create_camera)
assert ( assert (
entity_registry.async_get(camera.entity_id).unique_id entity_registry.async_get(camera.entity_id).unique_id
== f"00:00:00:00:00:00_{aid}" == f"00:00:00:00:00:00_{aid}"
) )
async def test_read_state(hass: HomeAssistant) -> None: async def test_read_state(hass: HomeAssistant, get_next_aid: Callable[[], int]) -> None:
"""Test reading the state of a HomeKit camera.""" """Test reading the state of a HomeKit camera."""
helper = await setup_test_component(hass, create_camera) helper = await setup_test_component(hass, get_next_aid(), create_camera)
state = await helper.poll_and_get_state() state = await helper.poll_and_get_state()
assert state.state == "idle" assert state.state == "idle"
async def test_get_image(hass: HomeAssistant) -> None: async def test_get_image(hass: HomeAssistant, get_next_aid: Callable[[], int]) -> None:
"""Test getting a JPEG from a camera.""" """Test getting a JPEG from a camera."""
helper = await setup_test_component(hass, create_camera) helper = await setup_test_component(hass, get_next_aid(), create_camera)
image = await camera.async_get_image(hass, helper.entity_id) image = await camera.async_get_image(hass, helper.entity_id)
assert image.content == base64.b64decode(FAKE_CAMERA_IMAGE) assert image.content == base64.b64decode(FAKE_CAMERA_IMAGE)

View file

@ -1,5 +1,7 @@
"""Basic checks for HomeKitclimate.""" """Basic checks for HomeKitclimate."""
from collections.abc import Callable
from aiohomekit.model.characteristics import ( from aiohomekit.model.characteristics import (
ActivationStateValues, ActivationStateValues,
CharacteristicsTypes, CharacteristicsTypes,
@ -21,7 +23,7 @@ from homeassistant.components.climate import (
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .common import get_next_aid, setup_test_component from .common import setup_test_component
# Test thermostat devices # Test thermostat devices
@ -73,9 +75,13 @@ def create_thermostat_service_min_max(accessory):
char.maxValue = 1 char.maxValue = 1
async def test_climate_respect_supported_op_modes_1(hass: HomeAssistant) -> None: async def test_climate_respect_supported_op_modes_1(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that climate respects minValue/maxValue hints.""" """Test that climate respects minValue/maxValue hints."""
helper = await setup_test_component(hass, create_thermostat_service_min_max) helper = await setup_test_component(
hass, get_next_aid(), create_thermostat_service_min_max
)
state = await helper.poll_and_get_state() state = await helper.poll_and_get_state()
assert state.attributes["hvac_modes"] == ["off", "heat"] assert state.attributes["hvac_modes"] == ["off", "heat"]
@ -88,16 +94,22 @@ def create_thermostat_service_valid_vals(accessory):
char.valid_values = [0, 1, 2] char.valid_values = [0, 1, 2]
async def test_climate_respect_supported_op_modes_2(hass: HomeAssistant) -> None: async def test_climate_respect_supported_op_modes_2(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that climate respects validValue hints.""" """Test that climate respects validValue hints."""
helper = await setup_test_component(hass, create_thermostat_service_valid_vals) helper = await setup_test_component(
hass, get_next_aid(), create_thermostat_service_valid_vals
)
state = await helper.poll_and_get_state() state = await helper.poll_and_get_state()
assert state.attributes["hvac_modes"] == ["off", "heat", "cool"] assert state.attributes["hvac_modes"] == ["off", "heat", "cool"]
async def test_climate_change_thermostat_state(hass: HomeAssistant) -> None: async def test_climate_change_thermostat_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a HomeKit thermostat on and off again.""" """Test that we can turn a HomeKit thermostat on and off again."""
helper = await setup_test_component(hass, create_thermostat_service) helper = await setup_test_component(hass, get_next_aid(), create_thermostat_service)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -178,9 +190,11 @@ async def test_climate_change_thermostat_state(hass: HomeAssistant) -> None:
) )
async def test_climate_check_min_max_values_per_mode(hass: HomeAssistant) -> None: async def test_climate_check_min_max_values_per_mode(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we we get the appropriate min/max values for each mode.""" """Test that we we get the appropriate min/max values for each mode."""
helper = await setup_test_component(hass, create_thermostat_service) helper = await setup_test_component(hass, get_next_aid(), create_thermostat_service)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -213,9 +227,11 @@ async def test_climate_check_min_max_values_per_mode(hass: HomeAssistant) -> Non
assert climate_state.attributes["max_temp"] == 40 assert climate_state.attributes["max_temp"] == 40
async def test_climate_change_thermostat_temperature(hass: HomeAssistant) -> None: async def test_climate_change_thermostat_temperature(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a HomeKit thermostat on and off again.""" """Test that we can turn a HomeKit thermostat on and off again."""
helper = await setup_test_component(hass, create_thermostat_service) helper = await setup_test_component(hass, get_next_aid(), create_thermostat_service)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -244,9 +260,11 @@ async def test_climate_change_thermostat_temperature(hass: HomeAssistant) -> Non
) )
async def test_climate_change_thermostat_temperature_range(hass: HomeAssistant) -> None: async def test_climate_change_thermostat_temperature_range(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can set separate heat and cool setpoints in heat_cool mode.""" """Test that we can set separate heat and cool setpoints in heat_cool mode."""
helper = await setup_test_component(hass, create_thermostat_service) helper = await setup_test_component(hass, get_next_aid(), create_thermostat_service)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -278,10 +296,10 @@ async def test_climate_change_thermostat_temperature_range(hass: HomeAssistant)
async def test_climate_change_thermostat_temperature_range_iphone( async def test_climate_change_thermostat_temperature_range_iphone(
hass: HomeAssistant, hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None: ) -> None:
"""Test that we can set all three set points at once (iPhone heat_cool mode support).""" """Test that we can set all three set points at once (iPhone heat_cool mode support)."""
helper = await setup_test_component(hass, create_thermostat_service) helper = await setup_test_component(hass, get_next_aid(), create_thermostat_service)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -313,10 +331,10 @@ async def test_climate_change_thermostat_temperature_range_iphone(
async def test_climate_cannot_set_thermostat_temp_range_in_wrong_mode( async def test_climate_cannot_set_thermostat_temp_range_in_wrong_mode(
hass: HomeAssistant, hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None: ) -> None:
"""Test that we cannot set range values when not in heat_cool mode.""" """Test that we cannot set range values when not in heat_cool mode."""
helper = await setup_test_component(hass, create_thermostat_service) helper = await setup_test_component(hass, get_next_aid(), create_thermostat_service)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -372,10 +390,12 @@ def create_thermostat_single_set_point_auto(accessory):
async def test_climate_check_min_max_values_per_mode_sspa_device( async def test_climate_check_min_max_values_per_mode_sspa_device(
hass: HomeAssistant, hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None: ) -> None:
"""Test appropriate min/max values for each mode on sspa devices.""" """Test appropriate min/max values for each mode on sspa devices."""
helper = await setup_test_component(hass, create_thermostat_single_set_point_auto) helper = await setup_test_component(
hass, get_next_aid(), create_thermostat_single_set_point_auto
)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -408,9 +428,13 @@ async def test_climate_check_min_max_values_per_mode_sspa_device(
assert climate_state.attributes["max_temp"] == 35 assert climate_state.attributes["max_temp"] == 35
async def test_climate_set_thermostat_temp_on_sspa_device(hass: HomeAssistant) -> None: async def test_climate_set_thermostat_temp_on_sspa_device(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test setting temperature in different modes on device with single set point in auto.""" """Test setting temperature in different modes on device with single set point in auto."""
helper = await setup_test_component(hass, create_thermostat_single_set_point_auto) helper = await setup_test_component(
hass, get_next_aid(), create_thermostat_single_set_point_auto
)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -462,9 +486,13 @@ async def test_climate_set_thermostat_temp_on_sspa_device(hass: HomeAssistant) -
) )
async def test_climate_set_mode_via_temp(hass: HomeAssistant) -> None: async def test_climate_set_mode_via_temp(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test setting temperature and mode at same tims.""" """Test setting temperature and mode at same tims."""
helper = await setup_test_component(hass, create_thermostat_single_set_point_auto) helper = await setup_test_component(
hass, get_next_aid(), create_thermostat_single_set_point_auto
)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -503,9 +531,11 @@ async def test_climate_set_mode_via_temp(hass: HomeAssistant) -> None:
) )
async def test_climate_change_thermostat_humidity(hass: HomeAssistant) -> None: async def test_climate_change_thermostat_humidity(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a HomeKit thermostat on and off again.""" """Test that we can turn a HomeKit thermostat on and off again."""
helper = await setup_test_component(hass, create_thermostat_service) helper = await setup_test_component(hass, get_next_aid(), create_thermostat_service)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -534,9 +564,11 @@ async def test_climate_change_thermostat_humidity(hass: HomeAssistant) -> None:
) )
async def test_climate_read_thermostat_state(hass: HomeAssistant) -> None: async def test_climate_read_thermostat_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit thermostat accessory.""" """Test that we can read the state of a HomeKit thermostat accessory."""
helper = await setup_test_component(hass, create_thermostat_service) helper = await setup_test_component(hass, get_next_aid(), create_thermostat_service)
# Simulate that heating is on # Simulate that heating is on
await helper.async_update( await helper.async_update(
@ -591,9 +623,11 @@ async def test_climate_read_thermostat_state(hass: HomeAssistant) -> None:
assert state.state == HVACMode.HEAT_COOL assert state.state == HVACMode.HEAT_COOL
async def test_hvac_mode_vs_hvac_action(hass: HomeAssistant) -> None: async def test_hvac_mode_vs_hvac_action(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Check that we haven't conflated hvac_mode and hvac_action.""" """Check that we haven't conflated hvac_mode and hvac_action."""
helper = await setup_test_component(hass, create_thermostat_service) helper = await setup_test_component(hass, get_next_aid(), create_thermostat_service)
# Simulate that current temperature is above target temp # Simulate that current temperature is above target temp
# Heating might be on, but hvac_action currently 'off' # Heating might be on, but hvac_action currently 'off'
@ -628,9 +662,11 @@ async def test_hvac_mode_vs_hvac_action(hass: HomeAssistant) -> None:
assert state.attributes["hvac_action"] == "heating" assert state.attributes["hvac_action"] == "heating"
async def test_hvac_mode_vs_hvac_action_current_mode_wrong(hass: HomeAssistant) -> None: async def test_hvac_mode_vs_hvac_action_current_mode_wrong(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Check that we cope with buggy HEATING_COOLING_CURRENT.""" """Check that we cope with buggy HEATING_COOLING_CURRENT."""
helper = await setup_test_component(hass, create_thermostat_service) helper = await setup_test_component(hass, get_next_aid(), create_thermostat_service)
await helper.async_update( await helper.async_update(
ServicesTypes.THERMOSTAT, ServicesTypes.THERMOSTAT,
@ -692,9 +728,13 @@ def create_heater_cooler_service_min_max(accessory):
char.maxValue = 2 char.maxValue = 2
async def test_heater_cooler_respect_supported_op_modes_1(hass: HomeAssistant) -> None: async def test_heater_cooler_respect_supported_op_modes_1(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that climate respects minValue/maxValue hints.""" """Test that climate respects minValue/maxValue hints."""
helper = await setup_test_component(hass, create_heater_cooler_service_min_max) helper = await setup_test_component(
hass, get_next_aid(), create_heater_cooler_service_min_max
)
state = await helper.poll_and_get_state() state = await helper.poll_and_get_state()
assert state.attributes["hvac_modes"] == ["heat", "cool", "off"] assert state.attributes["hvac_modes"] == ["heat", "cool", "off"]
@ -707,16 +747,24 @@ def create_theater_cooler_service_valid_vals(accessory):
char.valid_values = [1, 2] char.valid_values = [1, 2]
async def test_heater_cooler_respect_supported_op_modes_2(hass: HomeAssistant) -> None: async def test_heater_cooler_respect_supported_op_modes_2(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that climate respects validValue hints.""" """Test that climate respects validValue hints."""
helper = await setup_test_component(hass, create_theater_cooler_service_valid_vals) helper = await setup_test_component(
hass, get_next_aid(), create_theater_cooler_service_valid_vals
)
state = await helper.poll_and_get_state() state = await helper.poll_and_get_state()
assert state.attributes["hvac_modes"] == ["heat", "cool", "off"] assert state.attributes["hvac_modes"] == ["heat", "cool", "off"]
async def test_heater_cooler_change_thermostat_state(hass: HomeAssistant) -> None: async def test_heater_cooler_change_thermostat_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can change the operational mode.""" """Test that we can change the operational mode."""
helper = await setup_test_component(hass, create_heater_cooler_service) helper = await setup_test_component(
hass, get_next_aid(), create_heater_cooler_service
)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -771,12 +819,16 @@ async def test_heater_cooler_change_thermostat_state(hass: HomeAssistant) -> Non
) )
async def test_can_turn_on_after_off(hass: HomeAssistant) -> None: async def test_can_turn_on_after_off(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we always force device from inactive to active when setting mode. """Test that we always force device from inactive to active when setting mode.
This is a regression test for #81863. This is a regression test for #81863.
""" """
helper = await setup_test_component(hass, create_heater_cooler_service) helper = await setup_test_component(
hass, get_next_aid(), create_heater_cooler_service
)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -806,9 +858,13 @@ async def test_can_turn_on_after_off(hass: HomeAssistant) -> None:
) )
async def test_heater_cooler_change_thermostat_temperature(hass: HomeAssistant) -> None: async def test_heater_cooler_change_thermostat_temperature(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can change the target temperature.""" """Test that we can change the target temperature."""
helper = await setup_test_component(hass, create_heater_cooler_service) helper = await setup_test_component(
hass, get_next_aid(), create_heater_cooler_service
)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -849,9 +905,13 @@ async def test_heater_cooler_change_thermostat_temperature(hass: HomeAssistant)
) )
async def test_heater_cooler_change_fan_speed(hass: HomeAssistant) -> None: async def test_heater_cooler_change_fan_speed(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can change the target fan speed.""" """Test that we can change the target fan speed."""
helper = await setup_test_component(hass, create_heater_cooler_service) helper = await setup_test_component(
hass, get_next_aid(), create_heater_cooler_service
)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -897,9 +957,13 @@ async def test_heater_cooler_change_fan_speed(hass: HomeAssistant) -> None:
) )
async def test_heater_cooler_read_fan_speed(hass: HomeAssistant) -> None: async def test_heater_cooler_read_fan_speed(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit thermostat accessory.""" """Test that we can read the state of a HomeKit thermostat accessory."""
helper = await setup_test_component(hass, create_heater_cooler_service) helper = await setup_test_component(
hass, get_next_aid(), create_heater_cooler_service
)
# Simulate that fan speed is off # Simulate that fan speed is off
await helper.async_update( await helper.async_update(
@ -946,9 +1010,13 @@ async def test_heater_cooler_read_fan_speed(hass: HomeAssistant) -> None:
assert state.attributes["fan_mode"] == "high" assert state.attributes["fan_mode"] == "high"
async def test_heater_cooler_read_thermostat_state(hass: HomeAssistant) -> None: async def test_heater_cooler_read_thermostat_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit thermostat accessory.""" """Test that we can read the state of a HomeKit thermostat accessory."""
helper = await setup_test_component(hass, create_heater_cooler_service) helper = await setup_test_component(
hass, get_next_aid(), create_heater_cooler_service
)
# Simulate that heating is on # Simulate that heating is on
await helper.async_update( await helper.async_update(
@ -1000,9 +1068,13 @@ async def test_heater_cooler_read_thermostat_state(hass: HomeAssistant) -> None:
assert state.state == HVACMode.HEAT_COOL assert state.state == HVACMode.HEAT_COOL
async def test_heater_cooler_hvac_mode_vs_hvac_action(hass: HomeAssistant) -> None: async def test_heater_cooler_hvac_mode_vs_hvac_action(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Check that we haven't conflated hvac_mode and hvac_action.""" """Check that we haven't conflated hvac_mode and hvac_action."""
helper = await setup_test_component(hass, create_heater_cooler_service) helper = await setup_test_component(
hass, get_next_aid(), create_heater_cooler_service
)
# Simulate that current temperature is above target temp # Simulate that current temperature is above target temp
# Heating might be on, but hvac_action currently 'off' # Heating might be on, but hvac_action currently 'off'
@ -1039,9 +1111,13 @@ async def test_heater_cooler_hvac_mode_vs_hvac_action(hass: HomeAssistant) -> No
assert state.attributes["hvac_action"] == "heating" assert state.attributes["hvac_action"] == "heating"
async def test_heater_cooler_change_swing_mode(hass: HomeAssistant) -> None: async def test_heater_cooler_change_swing_mode(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can change the swing mode.""" """Test that we can change the swing mode."""
helper = await setup_test_component(hass, create_heater_cooler_service) helper = await setup_test_component(
hass, get_next_aid(), create_heater_cooler_service
)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -1070,9 +1146,13 @@ async def test_heater_cooler_change_swing_mode(hass: HomeAssistant) -> None:
) )
async def test_heater_cooler_turn_off(hass: HomeAssistant) -> None: async def test_heater_cooler_turn_off(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that both hvac_action and hvac_mode return "off" when turned off.""" """Test that both hvac_action and hvac_mode return "off" when turned off."""
helper = await setup_test_component(hass, create_heater_cooler_service) helper = await setup_test_component(
hass, get_next_aid(), create_heater_cooler_service
)
# Simulate that the device is turned off but CURRENT_HEATER_COOLER_STATE still returns HEATING/COOLING # Simulate that the device is turned off but CURRENT_HEATER_COOLER_STATE still returns HEATING/COOLING
await helper.async_update( await helper.async_update(
@ -1090,7 +1170,9 @@ async def test_heater_cooler_turn_off(hass: HomeAssistant) -> None:
async def test_migrate_unique_id( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we can migrate a switch unique id.""" """Test a we can migrate a switch unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -1099,7 +1181,7 @@ async def test_migrate_unique_id(
"homekit_controller", "homekit_controller",
f"homekit-00:00:00:00:00:00-{aid}-8", f"homekit-00:00:00:00:00:00-{aid}-8",
) )
await setup_test_component(hass, create_heater_cooler_service) await setup_test_component(hass, aid, create_heater_cooler_service)
assert ( assert (
entity_registry.async_get(climate_entry.entity_id).unique_id entity_registry.async_get(climate_entry.entity_id).unique_id
== f"00:00:00:00:00:00_{aid}_8" == f"00:00:00:00:00:00_{aid}_8"

View file

@ -211,13 +211,13 @@ def setup_mock_accessory(controller):
bridge = Accessories() bridge = Accessories()
accessory = Accessory.create_with_info( accessory = Accessory.create_with_info(
1,
name="Koogeek-LS1-20833F", name="Koogeek-LS1-20833F",
manufacturer="Koogeek", manufacturer="Koogeek",
model="LS1", model="LS1",
serial_number="12345", serial_number="12345",
firmware_revision="1.1", firmware_revision="1.1",
) )
accessory.aid = 1
service = accessory.add_service(ServicesTypes.LIGHTBULB) service = accessory.add_service(ServicesTypes.LIGHTBULB)
on_char = service.add_char(CharacteristicsTypes.ON) on_char = service.add_char(CharacteristicsTypes.ON)

View file

@ -1,5 +1,7 @@
"""Basic checks for HomeKitalarm_control_panel.""" """Basic checks for HomeKitalarm_control_panel."""
from collections.abc import Callable
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
@ -7,7 +9,7 @@ from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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_window_covering_service(accessory): def create_window_covering_service(accessory):
@ -113,9 +115,13 @@ def create_window_covering_service_with_none_tilt(accessory):
tilt_target.maxValue = 0 tilt_target.maxValue = 0
async def test_change_window_cover_state(hass: HomeAssistant) -> None: async def test_change_window_cover_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a HomeKit alarm on and off again.""" """Test that we can turn a HomeKit alarm on and off again."""
helper = await setup_test_component(hass, create_window_covering_service) helper = await setup_test_component(
hass, get_next_aid(), create_window_covering_service
)
await hass.services.async_call( await hass.services.async_call(
"cover", "open_cover", {"entity_id": helper.entity_id}, blocking=True "cover", "open_cover", {"entity_id": helper.entity_id}, blocking=True
@ -138,9 +144,13 @@ async def test_change_window_cover_state(hass: HomeAssistant) -> None:
) )
async def test_read_window_cover_state(hass: HomeAssistant) -> None: async def test_read_window_cover_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit alarm accessory.""" """Test that we can read the state of a HomeKit alarm accessory."""
helper = await setup_test_component(hass, create_window_covering_service) helper = await setup_test_component(
hass, get_next_aid(), create_window_covering_service
)
await helper.async_update( await helper.async_update(
ServicesTypes.WINDOW_COVERING, ServicesTypes.WINDOW_COVERING,
@ -171,10 +181,12 @@ async def test_read_window_cover_state(hass: HomeAssistant) -> None:
assert state.attributes["obstruction-detected"] is True assert state.attributes["obstruction-detected"] is True
async def test_read_window_cover_tilt_horizontal(hass: HomeAssistant) -> None: async def test_read_window_cover_tilt_horizontal(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that horizontal tilt is handled correctly.""" """Test that horizontal tilt is handled correctly."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_window_covering_service_with_h_tilt hass, get_next_aid(), create_window_covering_service_with_h_tilt
) )
await helper.async_update( await helper.async_update(
@ -186,10 +198,12 @@ async def test_read_window_cover_tilt_horizontal(hass: HomeAssistant) -> None:
assert state.attributes["current_tilt_position"] == 83 assert state.attributes["current_tilt_position"] == 83
async def test_read_window_cover_tilt_horizontal_2(hass: HomeAssistant) -> None: async def test_read_window_cover_tilt_horizontal_2(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that horizontal tilt is handled correctly.""" """Test that horizontal tilt is handled correctly."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_window_covering_service_with_h_tilt_2 hass, get_next_aid(), create_window_covering_service_with_h_tilt_2
) )
await helper.async_update( await helper.async_update(
@ -201,10 +215,12 @@ async def test_read_window_cover_tilt_horizontal_2(hass: HomeAssistant) -> None:
assert state.attributes["current_tilt_position"] == 83 assert state.attributes["current_tilt_position"] == 83
async def test_read_window_cover_tilt_vertical(hass: HomeAssistant) -> None: async def test_read_window_cover_tilt_vertical(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that vertical tilt is handled correctly.""" """Test that vertical tilt is handled correctly."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_window_covering_service_with_v_tilt hass, get_next_aid(), create_window_covering_service_with_v_tilt
) )
await helper.async_update( await helper.async_update(
@ -216,10 +232,12 @@ async def test_read_window_cover_tilt_vertical(hass: HomeAssistant) -> None:
assert state.attributes["current_tilt_position"] == 83 assert state.attributes["current_tilt_position"] == 83
async def test_read_window_cover_tilt_vertical_2(hass: HomeAssistant) -> None: async def test_read_window_cover_tilt_vertical_2(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that vertical tilt is handled correctly.""" """Test that vertical tilt is handled correctly."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_window_covering_service_with_v_tilt_2 hass, get_next_aid(), create_window_covering_service_with_v_tilt_2
) )
await helper.async_update( await helper.async_update(
@ -231,10 +249,12 @@ async def test_read_window_cover_tilt_vertical_2(hass: HomeAssistant) -> None:
assert state.attributes["current_tilt_position"] == 83 assert state.attributes["current_tilt_position"] == 83
async def test_read_window_cover_tilt_missing_tilt(hass: HomeAssistant) -> None: async def test_read_window_cover_tilt_missing_tilt(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that missing tilt is handled.""" """Test that missing tilt is handled."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_window_covering_service_with_none_tilt hass, get_next_aid(), create_window_covering_service_with_none_tilt
) )
await helper.async_update( await helper.async_update(
@ -246,10 +266,12 @@ async def test_read_window_cover_tilt_missing_tilt(hass: HomeAssistant) -> None:
assert state.state != STATE_UNAVAILABLE assert state.state != STATE_UNAVAILABLE
async def test_write_window_cover_tilt_horizontal(hass: HomeAssistant) -> None: async def test_write_window_cover_tilt_horizontal(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that horizontal tilt is written correctly.""" """Test that horizontal tilt is written correctly."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_window_covering_service_with_h_tilt hass, get_next_aid(), create_window_covering_service_with_h_tilt
) )
await hass.services.async_call( await hass.services.async_call(
@ -267,10 +289,12 @@ async def test_write_window_cover_tilt_horizontal(hass: HomeAssistant) -> None:
) )
async def test_write_window_cover_tilt_horizontal_2(hass: HomeAssistant) -> None: async def test_write_window_cover_tilt_horizontal_2(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that horizontal tilt is written correctly.""" """Test that horizontal tilt is written correctly."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_window_covering_service_with_h_tilt_2 hass, get_next_aid(), create_window_covering_service_with_h_tilt_2
) )
await hass.services.async_call( await hass.services.async_call(
@ -288,10 +312,12 @@ async def test_write_window_cover_tilt_horizontal_2(hass: HomeAssistant) -> None
) )
async def test_write_window_cover_tilt_vertical(hass: HomeAssistant) -> None: async def test_write_window_cover_tilt_vertical(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that vertical tilt is written correctly.""" """Test that vertical tilt is written correctly."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_window_covering_service_with_v_tilt hass, get_next_aid(), create_window_covering_service_with_v_tilt
) )
await hass.services.async_call( await hass.services.async_call(
@ -309,10 +335,12 @@ async def test_write_window_cover_tilt_vertical(hass: HomeAssistant) -> None:
) )
async def test_write_window_cover_tilt_vertical_2(hass: HomeAssistant) -> None: async def test_write_window_cover_tilt_vertical_2(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that vertical tilt is written correctly.""" """Test that vertical tilt is written correctly."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_window_covering_service_with_v_tilt_2 hass, get_next_aid(), create_window_covering_service_with_v_tilt_2
) )
await hass.services.async_call( await hass.services.async_call(
@ -330,10 +358,12 @@ async def test_write_window_cover_tilt_vertical_2(hass: HomeAssistant) -> None:
) )
async def test_window_cover_stop(hass: HomeAssistant) -> None: async def test_window_cover_stop(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that vertical tilt is written correctly.""" """Test that vertical tilt is written correctly."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_window_covering_service_with_v_tilt hass, get_next_aid(), create_window_covering_service_with_v_tilt
) )
await hass.services.async_call( await hass.services.async_call(
@ -366,9 +396,13 @@ def create_garage_door_opener_service(accessory):
return service return service
async def test_change_door_state(hass: HomeAssistant) -> None: async def test_change_door_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn open and close a HomeKit garage door.""" """Test that we can turn open and close a HomeKit garage door."""
helper = await setup_test_component(hass, create_garage_door_opener_service) helper = await setup_test_component(
hass, get_next_aid(), create_garage_door_opener_service
)
await hass.services.async_call( await hass.services.async_call(
"cover", "open_cover", {"entity_id": helper.entity_id}, blocking=True "cover", "open_cover", {"entity_id": helper.entity_id}, blocking=True
@ -391,9 +425,13 @@ async def test_change_door_state(hass: HomeAssistant) -> None:
) )
async def test_read_door_state(hass: HomeAssistant) -> None: async def test_read_door_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit garage door.""" """Test that we can read the state of a HomeKit garage door."""
helper = await setup_test_component(hass, create_garage_door_opener_service) helper = await setup_test_component(
hass, get_next_aid(), create_garage_door_opener_service
)
await helper.async_update( await helper.async_update(
ServicesTypes.GARAGE_DOOR_OPENER, ServicesTypes.GARAGE_DOOR_OPENER,
@ -432,7 +470,9 @@ async def test_read_door_state(hass: HomeAssistant) -> None:
async def test_migrate_unique_id( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we can migrate a cover unique id.""" """Test a we can migrate a cover unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -441,7 +481,7 @@ async def test_migrate_unique_id(
"homekit_controller", "homekit_controller",
f"homekit-00:00:00:00:00:00-{aid}-8", f"homekit-00:00:00:00:00:00-{aid}-8",
) )
await setup_test_component(hass, create_garage_door_opener_service) await setup_test_component(hass, aid, create_garage_door_opener_service)
assert ( assert (
entity_registry.async_get(cover_entry.entity_id).unique_id entity_registry.async_get(cover_entry.entity_id).unique_id

View file

@ -1,5 +1,7 @@
"""Test homekit_controller stateless triggers.""" """Test homekit_controller stateless triggers."""
from collections.abc import Callable
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
import pytest import pytest
@ -82,9 +84,10 @@ async def test_enumerate_remote(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test that remote is correctly enumerated.""" """Test that remote is correctly enumerated."""
await setup_test_component(hass, create_remote) await setup_test_component(hass, get_next_aid(), create_remote)
bat_sensor = entity_registry.async_get("sensor.testdevice_battery") bat_sensor = entity_registry.async_get("sensor.testdevice_battery")
identify_button = entity_registry.async_get("button.testdevice_identify") identify_button = entity_registry.async_get("button.testdevice_identify")
@ -133,9 +136,10 @@ async def test_enumerate_button(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test that a button is correctly enumerated.""" """Test that a button is correctly enumerated."""
await setup_test_component(hass, create_button) await setup_test_component(hass, get_next_aid(), create_button)
bat_sensor = entity_registry.async_get("sensor.testdevice_battery") bat_sensor = entity_registry.async_get("sensor.testdevice_battery")
identify_button = entity_registry.async_get("button.testdevice_identify") identify_button = entity_registry.async_get("button.testdevice_identify")
@ -183,9 +187,10 @@ async def test_enumerate_doorbell(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test that a button is correctly enumerated.""" """Test that a button is correctly enumerated."""
await setup_test_component(hass, create_doorbell) await setup_test_component(hass, get_next_aid(), create_doorbell)
bat_sensor = entity_registry.async_get("sensor.testdevice_battery") bat_sensor = entity_registry.async_get("sensor.testdevice_battery")
identify_button = entity_registry.async_get("button.testdevice_identify") identify_button = entity_registry.async_get("button.testdevice_identify")
@ -233,10 +238,11 @@ async def test_handle_events(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
service_calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test that events are handled.""" """Test that events are handled."""
helper = await setup_test_component(hass, create_remote) helper = await setup_test_component(hass, get_next_aid(), create_remote)
entry = entity_registry.async_get("sensor.testdevice_battery") entry = entity_registry.async_get("sensor.testdevice_battery")
@ -355,10 +361,11 @@ async def test_handle_events_late_setup(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
service_calls: list[ServiceCall], service_calls: list[ServiceCall],
) -> None: ) -> None:
"""Test that events are handled when setup happens after startup.""" """Test that events are handled when setup happens after startup."""
helper = await setup_test_component(hass, create_remote) helper = await setup_test_component(hass, get_next_aid(), create_remote)
entry = entity_registry.async_get("sensor.testdevice_battery") entry = entity_registry.async_get("sensor.testdevice_battery")

View file

@ -1,5 +1,7 @@
"""Test homekit_controller stateless triggers.""" """Test homekit_controller stateless triggers."""
from collections.abc import Callable
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
@ -65,9 +67,13 @@ def create_doorbell(accessory):
battery.add_char(CharacteristicsTypes.BATTERY_LEVEL) battery.add_char(CharacteristicsTypes.BATTERY_LEVEL)
async def test_remote(hass: HomeAssistant, entity_registry: er.EntityRegistry) -> None: async def test_remote(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None:
"""Test that remote is supported.""" """Test that remote is supported."""
helper = await setup_test_component(hass, create_remote) helper = await setup_test_component(hass, get_next_aid(), create_remote)
entities = [ entities = [
("event.testdevice_button_1", "Button 1"), ("event.testdevice_button_1", "Button 1"),
@ -108,9 +114,13 @@ async def test_remote(hass: HomeAssistant, entity_registry: er.EntityRegistry) -
assert state.attributes["event_type"] == "long_press" assert state.attributes["event_type"] == "long_press"
async def test_button(hass: HomeAssistant, entity_registry: er.EntityRegistry) -> None: async def test_button(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None:
"""Test that a button is correctly enumerated.""" """Test that a button is correctly enumerated."""
helper = await setup_test_component(hass, create_button) helper = await setup_test_component(hass, get_next_aid(), create_button)
entity_id = "event.testdevice_button_1" entity_id = "event.testdevice_button_1"
button = entity_registry.async_get(entity_id) button = entity_registry.async_get(entity_id)
@ -145,10 +155,12 @@ async def test_button(hass: HomeAssistant, entity_registry: er.EntityRegistry) -
async def test_doorbell( async def test_doorbell(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test that doorbell service is handled.""" """Test that doorbell service is handled."""
helper = await setup_test_component(hass, create_doorbell) helper = await setup_test_component(hass, get_next_aid(), create_doorbell)
entity_id = "event.testdevice_doorbell" entity_id = "event.testdevice_doorbell"
doorbell = entity_registry.async_get(entity_id) doorbell = entity_registry.async_get(entity_id)

View file

@ -1,12 +1,14 @@
"""Basic checks for HomeKit fans.""" """Basic checks for HomeKit fans."""
from collections.abc import Callable
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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_fan_service(accessory): def create_fan_service(accessory):
@ -90,9 +92,11 @@ def create_fanv2_service_without_rotation_speed(accessory):
swing_mode.value = 0 swing_mode.value = 0
async def test_fan_read_state(hass: HomeAssistant) -> None: async def test_fan_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit fan accessory.""" """Test that we can read the state of a HomeKit fan accessory."""
helper = await setup_test_component(hass, create_fan_service) helper = await setup_test_component(hass, get_next_aid(), create_fan_service)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.FAN, {CharacteristicsTypes.ON: False} ServicesTypes.FAN, {CharacteristicsTypes.ON: False}
@ -105,9 +109,9 @@ async def test_fan_read_state(hass: HomeAssistant) -> None:
assert state.state == "on" assert state.state == "on"
async def test_turn_on(hass: HomeAssistant) -> None: async def test_turn_on(hass: HomeAssistant, get_next_aid: Callable[[], int]) -> None:
"""Test that we can turn a fan on.""" """Test that we can turn a fan on."""
helper = await setup_test_component(hass, create_fan_service) helper = await setup_test_component(hass, get_next_aid(), create_fan_service)
await hass.services.async_call( await hass.services.async_call(
"fan", "fan",
@ -152,10 +156,12 @@ async def test_turn_on(hass: HomeAssistant) -> None:
) )
async def test_turn_on_off_without_rotation_speed(hass: HomeAssistant) -> None: async def test_turn_on_off_without_rotation_speed(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a fan on.""" """Test that we can turn a fan on."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_fanv2_service_without_rotation_speed hass, get_next_aid(), create_fanv2_service_without_rotation_speed
) )
await hass.services.async_call( await hass.services.async_call(
@ -185,9 +191,9 @@ async def test_turn_on_off_without_rotation_speed(hass: HomeAssistant) -> None:
) )
async def test_turn_off(hass: HomeAssistant) -> None: async def test_turn_off(hass: HomeAssistant, get_next_aid: Callable[[], int]) -> None:
"""Test that we can turn a fan off.""" """Test that we can turn a fan off."""
helper = await setup_test_component(hass, create_fan_service) helper = await setup_test_component(hass, get_next_aid(), create_fan_service)
await helper.async_update(ServicesTypes.FAN, {CharacteristicsTypes.ON: 1}) await helper.async_update(ServicesTypes.FAN, {CharacteristicsTypes.ON: 1})
@ -205,9 +211,9 @@ async def test_turn_off(hass: HomeAssistant) -> None:
) )
async def test_set_speed(hass: HomeAssistant) -> None: async def test_set_speed(hass: HomeAssistant, get_next_aid: Callable[[], int]) -> None:
"""Test that we set fan speed.""" """Test that we set fan speed."""
helper = await setup_test_component(hass, create_fan_service) helper = await setup_test_component(hass, get_next_aid(), create_fan_service)
await helper.async_update(ServicesTypes.FAN, {CharacteristicsTypes.ON: 1}) await helper.async_update(ServicesTypes.FAN, {CharacteristicsTypes.ON: 1})
@ -264,9 +270,11 @@ async def test_set_speed(hass: HomeAssistant) -> None:
) )
async def test_set_percentage(hass: HomeAssistant) -> None: async def test_set_percentage(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we set fan speed by percentage.""" """Test that we set fan speed by percentage."""
helper = await setup_test_component(hass, create_fan_service) helper = await setup_test_component(hass, get_next_aid(), create_fan_service)
await helper.async_update(ServicesTypes.FAN, {CharacteristicsTypes.ON: 1}) await helper.async_update(ServicesTypes.FAN, {CharacteristicsTypes.ON: 1})
@ -297,9 +305,9 @@ async def test_set_percentage(hass: HomeAssistant) -> None:
) )
async def test_speed_read(hass: HomeAssistant) -> None: async def test_speed_read(hass: HomeAssistant, get_next_aid: Callable[[], int]) -> None:
"""Test that we can read a fans oscillation.""" """Test that we can read a fans oscillation."""
helper = await setup_test_component(hass, create_fan_service) helper = await setup_test_component(hass, get_next_aid(), create_fan_service)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.FAN, ServicesTypes.FAN,
@ -337,9 +345,11 @@ async def test_speed_read(hass: HomeAssistant) -> None:
assert state.attributes["percentage"] == 0 assert state.attributes["percentage"] == 0
async def test_set_direction(hass: HomeAssistant) -> None: async def test_set_direction(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can set fan spin direction.""" """Test that we can set fan spin direction."""
helper = await setup_test_component(hass, create_fan_service) helper = await setup_test_component(hass, get_next_aid(), create_fan_service)
await hass.services.async_call( await hass.services.async_call(
"fan", "fan",
@ -368,9 +378,11 @@ async def test_set_direction(hass: HomeAssistant) -> None:
) )
async def test_direction_read(hass: HomeAssistant) -> None: async def test_direction_read(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read a fans oscillation.""" """Test that we can read a fans oscillation."""
helper = await setup_test_component(hass, create_fan_service) helper = await setup_test_component(hass, get_next_aid(), create_fan_service)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.FAN, {CharacteristicsTypes.ROTATION_DIRECTION: 0} ServicesTypes.FAN, {CharacteristicsTypes.ROTATION_DIRECTION: 0}
@ -383,9 +395,11 @@ async def test_direction_read(hass: HomeAssistant) -> None:
assert state.attributes["direction"] == "reverse" assert state.attributes["direction"] == "reverse"
async def test_fanv2_read_state(hass: HomeAssistant) -> None: async def test_fanv2_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit fan accessory.""" """Test that we can read the state of a HomeKit fan accessory."""
helper = await setup_test_component(hass, create_fanv2_service) helper = await setup_test_component(hass, get_next_aid(), create_fanv2_service)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: False} ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: False}
@ -398,9 +412,9 @@ async def test_fanv2_read_state(hass: HomeAssistant) -> None:
assert state.state == "on" assert state.state == "on"
async def test_v2_turn_on(hass: HomeAssistant) -> None: async def test_v2_turn_on(hass: HomeAssistant, get_next_aid: Callable[[], int]) -> None:
"""Test that we can turn a fan on.""" """Test that we can turn a fan on."""
helper = await setup_test_component(hass, create_fanv2_service) helper = await setup_test_component(hass, get_next_aid(), create_fanv2_service)
await hass.services.async_call( await hass.services.async_call(
"fan", "fan",
@ -473,9 +487,11 @@ async def test_v2_turn_on(hass: HomeAssistant) -> None:
) )
async def test_v2_turn_off(hass: HomeAssistant) -> None: async def test_v2_turn_off(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a fan off.""" """Test that we can turn a fan off."""
helper = await setup_test_component(hass, create_fanv2_service) helper = await setup_test_component(hass, get_next_aid(), create_fanv2_service)
await helper.async_update(ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: 1}) await helper.async_update(ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: 1})
@ -493,9 +509,11 @@ async def test_v2_turn_off(hass: HomeAssistant) -> None:
) )
async def test_v2_set_speed(hass: HomeAssistant) -> None: async def test_v2_set_speed(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we set fan speed.""" """Test that we set fan speed."""
helper = await setup_test_component(hass, create_fanv2_service) helper = await setup_test_component(hass, get_next_aid(), create_fanv2_service)
await helper.async_update(ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: 1}) await helper.async_update(ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: 1})
@ -552,9 +570,11 @@ async def test_v2_set_speed(hass: HomeAssistant) -> None:
) )
async def test_v2_set_percentage(hass: HomeAssistant) -> None: async def test_v2_set_percentage(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we set fan speed by percentage.""" """Test that we set fan speed by percentage."""
helper = await setup_test_component(hass, create_fanv2_service) helper = await setup_test_component(hass, get_next_aid(), create_fanv2_service)
await helper.async_update(ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: 1}) await helper.async_update(ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: 1})
@ -585,9 +605,13 @@ async def test_v2_set_percentage(hass: HomeAssistant) -> None:
) )
async def test_v2_set_percentage_with_min_step(hass: HomeAssistant) -> None: async def test_v2_set_percentage_with_min_step(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we set fan speed by percentage.""" """Test that we set fan speed by percentage."""
helper = await setup_test_component(hass, create_fanv2_service_with_min_step) helper = await setup_test_component(
hass, get_next_aid(), create_fanv2_service_with_min_step
)
await helper.async_update(ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: 1}) await helper.async_update(ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: 1})
@ -618,9 +642,11 @@ async def test_v2_set_percentage_with_min_step(hass: HomeAssistant) -> None:
) )
async def test_v2_speed_read(hass: HomeAssistant) -> None: async def test_v2_speed_read(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read a fans oscillation.""" """Test that we can read a fans oscillation."""
helper = await setup_test_component(hass, create_fanv2_service) helper = await setup_test_component(hass, get_next_aid(), create_fanv2_service)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.FAN_V2, ServicesTypes.FAN_V2,
@ -657,9 +683,11 @@ async def test_v2_speed_read(hass: HomeAssistant) -> None:
assert state.attributes["percentage"] == 0 assert state.attributes["percentage"] == 0
async def test_v2_set_direction(hass: HomeAssistant) -> None: async def test_v2_set_direction(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can set fan spin direction.""" """Test that we can set fan spin direction."""
helper = await setup_test_component(hass, create_fanv2_service) helper = await setup_test_component(hass, get_next_aid(), create_fanv2_service)
await hass.services.async_call( await hass.services.async_call(
"fan", "fan",
@ -688,9 +716,11 @@ async def test_v2_set_direction(hass: HomeAssistant) -> None:
) )
async def test_v2_direction_read(hass: HomeAssistant) -> None: async def test_v2_direction_read(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read a fans oscillation.""" """Test that we can read a fans oscillation."""
helper = await setup_test_component(hass, create_fanv2_service) helper = await setup_test_component(hass, get_next_aid(), create_fanv2_service)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.FAN_V2, {CharacteristicsTypes.ROTATION_DIRECTION: 0} ServicesTypes.FAN_V2, {CharacteristicsTypes.ROTATION_DIRECTION: 0}
@ -703,9 +733,11 @@ async def test_v2_direction_read(hass: HomeAssistant) -> None:
assert state.attributes["direction"] == "reverse" assert state.attributes["direction"] == "reverse"
async def test_v2_oscillate(hass: HomeAssistant) -> None: async def test_v2_oscillate(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can control a fans oscillation.""" """Test that we can control a fans oscillation."""
helper = await setup_test_component(hass, create_fanv2_service) helper = await setup_test_component(hass, get_next_aid(), create_fanv2_service)
await hass.services.async_call( await hass.services.async_call(
"fan", "fan",
@ -734,9 +766,11 @@ async def test_v2_oscillate(hass: HomeAssistant) -> None:
) )
async def test_v2_oscillate_read(hass: HomeAssistant) -> None: async def test_v2_oscillate_read(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read a fans oscillation.""" """Test that we can read a fans oscillation."""
helper = await setup_test_component(hass, create_fanv2_service) helper = await setup_test_component(hass, get_next_aid(), create_fanv2_service)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.FAN_V2, {CharacteristicsTypes.SWING_MODE: 0} ServicesTypes.FAN_V2, {CharacteristicsTypes.SWING_MODE: 0}
@ -750,11 +784,11 @@ async def test_v2_oscillate_read(hass: HomeAssistant) -> None:
async def test_v2_set_percentage_non_standard_rotation_range( async def test_v2_set_percentage_non_standard_rotation_range(
hass: HomeAssistant, hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None: ) -> None:
"""Test that we set fan speed with a non-standard rotation range.""" """Test that we set fan speed with a non-standard rotation range."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_fanv2_service_non_standard_rotation_range hass, get_next_aid(), create_fanv2_service_non_standard_rotation_range
) )
await helper.async_update(ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: 1}) await helper.async_update(ServicesTypes.FAN_V2, {CharacteristicsTypes.ACTIVE: 1})
@ -813,7 +847,9 @@ async def test_v2_set_percentage_non_standard_rotation_range(
async def test_migrate_unique_id( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we can migrate a fan unique id.""" """Test a we can migrate a fan unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -822,7 +858,9 @@ async def test_migrate_unique_id(
"homekit_controller", "homekit_controller",
f"homekit-00:00:00:00:00:00-{aid}-8", f"homekit-00:00:00:00:00:00-{aid}-8",
) )
await setup_test_component(hass, create_fanv2_service_non_standard_rotation_range) await setup_test_component(
hass, aid, create_fanv2_service_non_standard_rotation_range
)
assert ( assert (
entity_registry.async_get(fan_entry.entity_id).unique_id entity_registry.async_get(fan_entry.entity_id).unique_id

View file

@ -1,5 +1,7 @@
"""Basic checks for HomeKit Humidifier/Dehumidifier.""" """Basic checks for HomeKit Humidifier/Dehumidifier."""
from collections.abc import Callable
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
@ -7,7 +9,7 @@ from homeassistant.components.humidifier import DOMAIN, MODE_AUTO, MODE_NORMAL
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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_humidifier_service(accessory): def create_humidifier_service(accessory):
@ -64,9 +66,11 @@ def create_dehumidifier_service(accessory):
return service return service
async def test_humidifier_active_state(hass: HomeAssistant) -> None: async def test_humidifier_active_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a HomeKit humidifier on and off again.""" """Test that we can turn a HomeKit humidifier on and off again."""
helper = await setup_test_component(hass, create_humidifier_service) helper = await setup_test_component(hass, get_next_aid(), create_humidifier_service)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, "turn_on", {"entity_id": helper.entity_id}, blocking=True DOMAIN, "turn_on", {"entity_id": helper.entity_id}, blocking=True
@ -87,9 +91,13 @@ async def test_humidifier_active_state(hass: HomeAssistant) -> None:
) )
async def test_dehumidifier_active_state(hass: HomeAssistant) -> None: async def test_dehumidifier_active_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a HomeKit dehumidifier on and off again.""" """Test that we can turn a HomeKit dehumidifier on and off again."""
helper = await setup_test_component(hass, create_dehumidifier_service) helper = await setup_test_component(
hass, get_next_aid(), create_dehumidifier_service
)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, "turn_on", {"entity_id": helper.entity_id}, blocking=True DOMAIN, "turn_on", {"entity_id": helper.entity_id}, blocking=True
@ -110,9 +118,11 @@ async def test_dehumidifier_active_state(hass: HomeAssistant) -> None:
) )
async def test_humidifier_read_humidity(hass: HomeAssistant) -> None: async def test_humidifier_read_humidity(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit humidifier accessory.""" """Test that we can read the state of a HomeKit humidifier accessory."""
helper = await setup_test_component(hass, create_humidifier_service) helper = await setup_test_component(hass, get_next_aid(), create_humidifier_service)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.HUMIDIFIER_DEHUMIDIFIER, ServicesTypes.HUMIDIFIER_DEHUMIDIFIER,
@ -149,9 +159,13 @@ async def test_humidifier_read_humidity(hass: HomeAssistant) -> None:
assert state.state == "off" assert state.state == "off"
async def test_dehumidifier_read_humidity(hass: HomeAssistant) -> None: async def test_dehumidifier_read_humidity(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit dehumidifier accessory.""" """Test that we can read the state of a HomeKit dehumidifier accessory."""
helper = await setup_test_component(hass, create_dehumidifier_service) helper = await setup_test_component(
hass, get_next_aid(), create_dehumidifier_service
)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.HUMIDIFIER_DEHUMIDIFIER, ServicesTypes.HUMIDIFIER_DEHUMIDIFIER,
@ -186,9 +200,11 @@ async def test_dehumidifier_read_humidity(hass: HomeAssistant) -> None:
assert state.attributes["humidity"] == 40 assert state.attributes["humidity"] == 40
async def test_humidifier_set_humidity(hass: HomeAssistant) -> None: async def test_humidifier_set_humidity(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can set the state of a HomeKit humidifier accessory.""" """Test that we can set the state of a HomeKit humidifier accessory."""
helper = await setup_test_component(hass, create_humidifier_service) helper = await setup_test_component(hass, get_next_aid(), create_humidifier_service)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -202,9 +218,13 @@ async def test_humidifier_set_humidity(hass: HomeAssistant) -> None:
) )
async def test_dehumidifier_set_humidity(hass: HomeAssistant) -> None: async def test_dehumidifier_set_humidity(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can set the state of a HomeKit dehumidifier accessory.""" """Test that we can set the state of a HomeKit dehumidifier accessory."""
helper = await setup_test_component(hass, create_dehumidifier_service) helper = await setup_test_component(
hass, get_next_aid(), create_dehumidifier_service
)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -218,9 +238,11 @@ async def test_dehumidifier_set_humidity(hass: HomeAssistant) -> None:
) )
async def test_humidifier_set_mode(hass: HomeAssistant) -> None: async def test_humidifier_set_mode(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can set the mode of a HomeKit humidifier accessory.""" """Test that we can set the mode of a HomeKit humidifier accessory."""
helper = await setup_test_component(hass, create_humidifier_service) helper = await setup_test_component(hass, get_next_aid(), create_humidifier_service)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -251,9 +273,13 @@ async def test_humidifier_set_mode(hass: HomeAssistant) -> None:
) )
async def test_dehumidifier_set_mode(hass: HomeAssistant) -> None: async def test_dehumidifier_set_mode(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can set the mode of a HomeKit dehumidifier accessory.""" """Test that we can set the mode of a HomeKit dehumidifier accessory."""
helper = await setup_test_component(hass, create_dehumidifier_service) helper = await setup_test_component(
hass, get_next_aid(), create_dehumidifier_service
)
await hass.services.async_call( await hass.services.async_call(
DOMAIN, DOMAIN,
@ -284,9 +310,11 @@ async def test_dehumidifier_set_mode(hass: HomeAssistant) -> None:
) )
async def test_humidifier_read_only_mode(hass: HomeAssistant) -> None: async def test_humidifier_read_only_mode(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit humidifier accessory.""" """Test that we can read the state of a HomeKit humidifier accessory."""
helper = await setup_test_component(hass, create_humidifier_service) helper = await setup_test_component(hass, get_next_aid(), create_humidifier_service)
state = await helper.poll_and_get_state() state = await helper.poll_and_get_state()
assert state.attributes["mode"] == "normal" assert state.attributes["mode"] == "normal"
@ -324,9 +352,13 @@ async def test_humidifier_read_only_mode(hass: HomeAssistant) -> None:
assert state.attributes["mode"] == "normal" assert state.attributes["mode"] == "normal"
async def test_dehumidifier_read_only_mode(hass: HomeAssistant) -> None: async def test_dehumidifier_read_only_mode(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit dehumidifier accessory.""" """Test that we can read the state of a HomeKit dehumidifier accessory."""
helper = await setup_test_component(hass, create_dehumidifier_service) helper = await setup_test_component(
hass, get_next_aid(), create_dehumidifier_service
)
state = await helper.poll_and_get_state() state = await helper.poll_and_get_state()
assert state.attributes["mode"] == "normal" assert state.attributes["mode"] == "normal"
@ -364,9 +396,11 @@ async def test_dehumidifier_read_only_mode(hass: HomeAssistant) -> None:
assert state.attributes["mode"] == "normal" assert state.attributes["mode"] == "normal"
async def test_humidifier_target_humidity_modes(hass: HomeAssistant) -> None: async def test_humidifier_target_humidity_modes(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit humidifier accessory.""" """Test that we can read the state of a HomeKit humidifier accessory."""
helper = await setup_test_component(hass, create_humidifier_service) helper = await setup_test_component(hass, get_next_aid(), create_humidifier_service)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.HUMIDIFIER_DEHUMIDIFIER, ServicesTypes.HUMIDIFIER_DEHUMIDIFIER,
@ -409,9 +443,13 @@ async def test_humidifier_target_humidity_modes(hass: HomeAssistant) -> None:
assert state.attributes["humidity"] == 37 assert state.attributes["humidity"] == 37
async def test_dehumidifier_target_humidity_modes(hass: HomeAssistant) -> None: async def test_dehumidifier_target_humidity_modes(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit dehumidifier accessory.""" """Test that we can read the state of a HomeKit dehumidifier accessory."""
helper = await setup_test_component(hass, create_dehumidifier_service) helper = await setup_test_component(
hass, get_next_aid(), create_dehumidifier_service
)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.HUMIDIFIER_DEHUMIDIFIER, ServicesTypes.HUMIDIFIER_DEHUMIDIFIER,
@ -457,7 +495,9 @@ async def test_dehumidifier_target_humidity_modes(hass: HomeAssistant) -> None:
async def test_migrate_entity_ids( async def test_migrate_entity_ids(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test that we can migrate humidifier entity ids.""" """Test that we can migrate humidifier entity ids."""
aid = get_next_aid() aid = get_next_aid()
@ -467,7 +507,7 @@ async def test_migrate_entity_ids(
"homekit_controller", "homekit_controller",
f"homekit-00:00:00:00:00:00-{aid}-8", f"homekit-00:00:00:00:00:00-{aid}-8",
) )
await setup_test_component(hass, create_humidifier_service) await setup_test_component(hass, aid, create_humidifier_service)
assert ( assert (
entity_registry.async_get(humidifier_entry.entity_id).unique_id entity_registry.async_get(humidifier_entry.entity_id).unique_id
== f"00:00:00:00:00:00_{aid}_8" == f"00:00:00:00:00:00_{aid}_8"

View file

@ -1,5 +1,6 @@
"""Tests for homekit_controller init.""" """Tests for homekit_controller init."""
from collections.abc import Callable
from datetime import timedelta from datetime import timedelta
import pathlib import pathlib
from unittest.mock import patch from unittest.mock import patch
@ -46,9 +47,11 @@ def create_motion_sensor_service(accessory):
cur_state.value = 0 cur_state.value = 0
async def test_unload_on_stop(hass: HomeAssistant) -> None: async def test_unload_on_stop(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test async_unload is called on stop.""" """Test async_unload is called on stop."""
await setup_test_component(hass, create_motion_sensor_service) await setup_test_component(hass, get_next_aid(), create_motion_sensor_service)
with patch( with patch(
"homeassistant.components.homekit_controller.HKDevice.async_unload" "homeassistant.components.homekit_controller.HKDevice.async_unload"
) as async_unlock_mock: ) as async_unlock_mock:
@ -58,9 +61,13 @@ async def test_unload_on_stop(hass: HomeAssistant) -> None:
assert async_unlock_mock.called assert async_unlock_mock.called
async def test_async_remove_entry(hass: HomeAssistant) -> None: async def test_async_remove_entry(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test unpairing a component.""" """Test unpairing a component."""
helper = await setup_test_component(hass, create_motion_sensor_service) helper = await setup_test_component(
hass, get_next_aid(), create_motion_sensor_service
)
controller = helper.pairing.controller controller = helper.pairing.controller
hkid = "00:00:00:00:00:00" hkid = "00:00:00:00:00:00"
@ -88,10 +95,13 @@ async def test_device_remove_devices(
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test we can only remove a device that no longer exists.""" """Test we can only remove a device that no longer exists."""
assert await async_setup_component(hass, "config", {}) assert await async_setup_component(hass, "config", {})
helper: Helper = await setup_test_component(hass, create_alive_service) helper: Helper = await setup_test_component(
hass, get_next_aid(), create_alive_service
)
config_entry = helper.config_entry config_entry = helper.config_entry
entry_id = config_entry.entry_id entry_id = config_entry.entry_id
@ -110,10 +120,13 @@ async def test_device_remove_devices(
assert response["success"] assert response["success"]
async def test_offline_device_raises(hass: HomeAssistant, controller) -> None: async def test_offline_device_raises(
hass: HomeAssistant, get_next_aid: Callable[[], int], controller
) -> None:
"""Test an offline device raises ConfigEntryNotReady.""" """Test an offline device raises ConfigEntryNotReady."""
is_connected = False is_connected = False
aid = get_next_aid()
class OfflineFakePairing(FakePairing): class OfflineFakePairing(FakePairing):
"""Fake pairing that can flip is_connected.""" """Fake pairing that can flip is_connected."""
@ -140,7 +153,7 @@ async def test_offline_device_raises(hass: HomeAssistant, controller) -> None:
return {} return {}
accessory = Accessory.create_with_info( accessory = Accessory.create_with_info(
"TestDevice", "example.com", "Test", "0001", "0.1" aid, "TestDevice", "example.com", "Test", "0001", "0.1"
) )
create_alive_service(accessory) create_alive_service(accessory)
@ -162,11 +175,12 @@ async def test_offline_device_raises(hass: HomeAssistant, controller) -> None:
async def test_ble_device_only_checks_is_available( async def test_ble_device_only_checks_is_available(
hass: HomeAssistant, controller hass: HomeAssistant, get_next_aid: Callable[[], int], controller
) -> None: ) -> None:
"""Test a BLE device only checks is_available.""" """Test a BLE device only checks is_available."""
is_available = False is_available = False
aid = get_next_aid()
class FakeBLEPairing(FakePairing): class FakeBLEPairing(FakePairing):
"""Fake BLE pairing that can flip is_available.""" """Fake BLE pairing that can flip is_available."""
@ -197,7 +211,7 @@ async def test_ble_device_only_checks_is_available(
return {} return {}
accessory = Accessory.create_with_info( accessory = Accessory.create_with_info(
"TestDevice", "example.com", "Test", "0001", "0.1" aid, "TestDevice", "example.com", "Test", "0001", "0.1"
) )
create_alive_service(accessory) create_alive_service(accessory)

View file

@ -1,5 +1,7 @@
"""Basic checks for HomeKitSwitch.""" """Basic checks for HomeKitSwitch."""
from collections.abc import Callable
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes 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.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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_NAME = "TestDevice"
LIGHT_BULB_ENTITY_ID = "light.testdevice" LIGHT_BULB_ENTITY_ID = "light.testdevice"
@ -55,9 +57,13 @@ def create_lightbulb_service_with_color_temp(accessory):
return service 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.""" """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( await hass.services.async_call(
"light", "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.""" """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( await hass.services.async_call(
"light", "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.""" """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 # Initial state is that the light is off
state = await helper.poll_and_get_state() 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" 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.""" """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 # Initial state is that the light is off
state = hass.states.get(LIGHT_BULB_ENTITY_ID) 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" 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.""" """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 # Initial state is that the light is off
state = await helper.poll_and_get_state() 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 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.""" """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 # Initial state is that the light is off
state = hass.states.get(LIGHT_BULB_ENTITY_ID) 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" 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.""" """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 # Initial state is that the light is off
state = await helper.poll_and_get_state() 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 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.""" """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 # Initial state is that the light is off
state = hass.states.get(LIGHT_BULB_ENTITY_ID) 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 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.""" """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 # Initial state is that the light is off
state = await helper.poll_and_get_state() 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 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.""" """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 # Initial state is that the light is off
state = await helper.poll_and_get_state() 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( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we can migrate a light unique id.""" """Test a we can migrate a light unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -391,7 +431,7 @@ async def test_migrate_unique_id(
"homekit_controller", "homekit_controller",
f"homekit-00:00:00:00:00:00-{aid}-8", 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 ( assert (
entity_registry.async_get(light_entry.entity_id).unique_id 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( async def test_only_migrate_once(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we handle migration happening after an upgrade and than a downgrade and then an upgrade.""" """Test a we handle migration happening after an upgrade and than a downgrade and then an upgrade."""
aid = get_next_aid() aid = get_next_aid()
@ -414,7 +456,7 @@ async def test_only_migrate_once(
"homekit_controller", "homekit_controller",
f"00:00:00:00:00:00_{aid}_8", 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 ( assert (
entity_registry.async_get(old_light_entry.entity_id).unique_id entity_registry.async_get(old_light_entry.entity_id).unique_id

View file

@ -1,12 +1,14 @@
"""Basic checks for HomeKitLock.""" """Basic checks for HomeKitLock."""
from collections.abc import Callable
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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_lock_service(accessory): def create_lock_service(accessory):
@ -29,9 +31,11 @@ def create_lock_service(accessory):
return service return service
async def test_switch_change_lock_state(hass: HomeAssistant) -> None: async def test_switch_change_lock_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can turn a HomeKit lock on and off again.""" """Test that we can turn a HomeKit lock on and off again."""
helper = await setup_test_component(hass, create_lock_service) helper = await setup_test_component(hass, get_next_aid(), create_lock_service)
await hass.services.async_call( await hass.services.async_call(
"lock", "lock", {"entity_id": "lock.testdevice"}, blocking=True "lock", "lock", {"entity_id": "lock.testdevice"}, blocking=True
@ -54,9 +58,11 @@ async def test_switch_change_lock_state(hass: HomeAssistant) -> None:
) )
async def test_switch_read_lock_state(hass: HomeAssistant) -> None: async def test_switch_read_lock_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit lock accessory.""" """Test that we can read the state of a HomeKit lock accessory."""
helper = await setup_test_component(hass, create_lock_service) helper = await setup_test_component(hass, get_next_aid(), create_lock_service)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.LOCK_MECHANISM, ServicesTypes.LOCK_MECHANISM,
@ -119,7 +125,9 @@ async def test_switch_read_lock_state(hass: HomeAssistant) -> None:
async def test_migrate_unique_id( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we can migrate a lock unique id.""" """Test a we can migrate a lock unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -128,7 +136,7 @@ async def test_migrate_unique_id(
"homekit_controller", "homekit_controller",
f"homekit-00:00:00:00:00:00-{aid}-8", f"homekit-00:00:00:00:00:00-{aid}-8",
) )
await setup_test_component(hass, create_lock_service) await setup_test_component(hass, aid, create_lock_service)
assert ( assert (
entity_registry.async_get(lock_entry.entity_id).unique_id entity_registry.async_get(lock_entry.entity_id).unique_id

View file

@ -1,5 +1,7 @@
"""Basic checks for HomeKit motion sensors and contact sensors.""" """Basic checks for HomeKit motion sensors and contact sensors."""
from collections.abc import Callable
from aiohomekit.model.characteristics import ( from aiohomekit.model.characteristics import (
CharacteristicPermissions, CharacteristicPermissions,
CharacteristicsTypes, CharacteristicsTypes,
@ -10,7 +12,7 @@ import pytest
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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_tv_service(accessory): def create_tv_service(accessory):
@ -62,9 +64,11 @@ def create_tv_service_with_target_media_state(accessory):
return service return service
async def test_tv_read_state(hass: HomeAssistant) -> None: async def test_tv_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the state of a HomeKit fan accessory.""" """Test that we can read the state of a HomeKit fan accessory."""
helper = await setup_test_component(hass, create_tv_service) helper = await setup_test_component(hass, get_next_aid(), create_tv_service)
state = await helper.async_update( state = await helper.async_update(
ServicesTypes.TELEVISION, ServicesTypes.TELEVISION,
@ -91,18 +95,22 @@ async def test_tv_read_state(hass: HomeAssistant) -> None:
assert state.state == "idle" assert state.state == "idle"
async def test_tv_read_sources(hass: HomeAssistant) -> None: async def test_tv_read_sources(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can read the input source of a HomeKit TV.""" """Test that we can read the input source of a HomeKit TV."""
helper = await setup_test_component(hass, create_tv_service) helper = await setup_test_component(hass, get_next_aid(), create_tv_service)
state = await helper.poll_and_get_state() state = await helper.poll_and_get_state()
assert state.attributes["source"] == "HDMI 1" assert state.attributes["source"] == "HDMI 1"
assert state.attributes["source_list"] == ["HDMI 1", "HDMI 2"] assert state.attributes["source_list"] == ["HDMI 1", "HDMI 2"]
async def test_play_remote_key(hass: HomeAssistant) -> None: async def test_play_remote_key(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can play media on a media player.""" """Test that we can play media on a media player."""
helper = await setup_test_component(hass, create_tv_service) helper = await setup_test_component(hass, get_next_aid(), create_tv_service)
await helper.async_update( await helper.async_update(
ServicesTypes.TELEVISION, ServicesTypes.TELEVISION,
@ -147,9 +155,11 @@ async def test_play_remote_key(hass: HomeAssistant) -> None:
) )
async def test_pause_remote_key(hass: HomeAssistant) -> None: async def test_pause_remote_key(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can pause a media player.""" """Test that we can pause a media player."""
helper = await setup_test_component(hass, create_tv_service) helper = await setup_test_component(hass, get_next_aid(), create_tv_service)
await helper.async_update( await helper.async_update(
ServicesTypes.TELEVISION, ServicesTypes.TELEVISION,
@ -194,9 +204,11 @@ async def test_pause_remote_key(hass: HomeAssistant) -> None:
) )
async def test_play(hass: HomeAssistant) -> None: async def test_play(hass: HomeAssistant, get_next_aid: Callable[[], int]) -> None:
"""Test that we can play media on a media player.""" """Test that we can play media on a media player."""
helper = await setup_test_component(hass, create_tv_service_with_target_media_state) helper = await setup_test_component(
hass, get_next_aid(), create_tv_service_with_target_media_state
)
await helper.async_update( await helper.async_update(
ServicesTypes.TELEVISION, ServicesTypes.TELEVISION,
@ -243,9 +255,11 @@ async def test_play(hass: HomeAssistant) -> None:
) )
async def test_pause(hass: HomeAssistant) -> None: async def test_pause(hass: HomeAssistant, get_next_aid: Callable[[], int]) -> None:
"""Test that we can turn pause a media player.""" """Test that we can turn pause a media player."""
helper = await setup_test_component(hass, create_tv_service_with_target_media_state) helper = await setup_test_component(
hass, get_next_aid(), create_tv_service_with_target_media_state
)
await helper.async_update( await helper.async_update(
ServicesTypes.TELEVISION, ServicesTypes.TELEVISION,
@ -291,9 +305,11 @@ async def test_pause(hass: HomeAssistant) -> None:
) )
async def test_stop(hass: HomeAssistant) -> None: async def test_stop(hass: HomeAssistant, get_next_aid: Callable[[], int]) -> None:
"""Test that we can stop a media player.""" """Test that we can stop a media player."""
helper = await setup_test_component(hass, create_tv_service_with_target_media_state) helper = await setup_test_component(
hass, get_next_aid(), create_tv_service_with_target_media_state
)
await hass.services.async_call( await hass.services.async_call(
"media_player", "media_player",
@ -332,9 +348,11 @@ async def test_stop(hass: HomeAssistant) -> None:
) )
async def test_tv_set_source(hass: HomeAssistant) -> None: async def test_tv_set_source(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can set the input source of a HomeKit TV.""" """Test that we can set the input source of a HomeKit TV."""
helper = await setup_test_component(hass, create_tv_service) helper = await setup_test_component(hass, get_next_aid(), create_tv_service)
await hass.services.async_call( await hass.services.async_call(
"media_player", "media_player",
@ -353,9 +371,11 @@ async def test_tv_set_source(hass: HomeAssistant) -> None:
assert state.attributes["source"] == "HDMI 2" assert state.attributes["source"] == "HDMI 2"
async def test_tv_set_source_fail(hass: HomeAssistant) -> None: async def test_tv_set_source_fail(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that we can set the input source of a HomeKit TV.""" """Test that we can set the input source of a HomeKit TV."""
helper = await setup_test_component(hass, create_tv_service) helper = await setup_test_component(hass, get_next_aid(), create_tv_service)
with pytest.raises(ValueError): with pytest.raises(ValueError):
await hass.services.async_call( await hass.services.async_call(
@ -370,7 +390,9 @@ async def test_tv_set_source_fail(hass: HomeAssistant) -> None:
async def test_migrate_unique_id( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we can migrate a media_player unique id.""" """Test a we can migrate a media_player unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -379,7 +401,7 @@ async def test_migrate_unique_id(
"homekit_controller", "homekit_controller",
f"homekit-00:00:00:00:00:00-{aid}-8", f"homekit-00:00:00:00:00:00-{aid}-8",
) )
await setup_test_component(hass, create_tv_service_with_target_media_state) await setup_test_component(hass, aid, create_tv_service_with_target_media_state)
assert ( assert (
entity_registry.async_get(media_player_entry.entity_id).unique_id entity_registry.async_get(media_player_entry.entity_id).unique_id

View file

@ -1,12 +1,14 @@
"""Basic checks for HomeKit sensor.""" """Basic checks for HomeKit sensor."""
from collections.abc import Callable
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes from aiohomekit.model.services import ServicesTypes
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .common import Helper, get_next_aid, setup_test_component from .common import Helper, setup_test_component
def create_switch_with_spray_level(accessory): def create_switch_with_spray_level(accessory):
@ -31,7 +33,9 @@ def create_switch_with_spray_level(accessory):
async def test_migrate_unique_id( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we can migrate a number unique id.""" """Test a we can migrate a number unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -41,7 +45,7 @@ async def test_migrate_unique_id(
f"homekit-0001-aid:{aid}-sid:8-cid:9", f"homekit-0001-aid:{aid}-sid:8-cid:9",
suggested_object_id="testdevice_spray_quantity", suggested_object_id="testdevice_spray_quantity",
) )
await setup_test_component(hass, create_switch_with_spray_level) await setup_test_component(hass, aid, create_switch_with_spray_level)
assert ( assert (
entity_registry.async_get(number.entity_id).unique_id entity_registry.async_get(number.entity_id).unique_id
@ -49,9 +53,13 @@ async def test_migrate_unique_id(
) )
async def test_read_number(hass: HomeAssistant) -> None: async def test_read_number(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test a switch service that has a sensor characteristic is correctly handled.""" """Test a switch service that has a sensor characteristic is correctly handled."""
helper = await setup_test_component(hass, create_switch_with_spray_level) helper = await setup_test_component(
hass, get_next_aid(), create_switch_with_spray_level
)
# Helper will be for the primary entity, which is the outlet. Make a helper for the sensor. # Helper will be for the primary entity, which is the outlet. Make a helper for the sensor.
spray_level = Helper( spray_level = Helper(
@ -75,9 +83,13 @@ async def test_read_number(hass: HomeAssistant) -> None:
assert state.state == "5" assert state.state == "5"
async def test_write_number(hass: HomeAssistant) -> None: async def test_write_number(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test a switch service that has a sensor characteristic is correctly handled.""" """Test a switch service that has a sensor characteristic is correctly handled."""
helper = await setup_test_component(hass, create_switch_with_spray_level) helper = await setup_test_component(
hass, get_next_aid(), create_switch_with_spray_level
)
# Helper will be for the primary entity, which is the outlet. Make a helper for the sensor. # Helper will be for the primary entity, which is the outlet. Make a helper for the sensor.
spray_level = Helper( spray_level = Helper(

View file

@ -1,5 +1,7 @@
"""Basic checks for HomeKit select entities.""" """Basic checks for HomeKit select entities."""
from collections.abc import Callable
from aiohomekit.model import Accessory from aiohomekit.model import Accessory
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.characteristics.const import TemperatureDisplayUnits from aiohomekit.model.characteristics.const import TemperatureDisplayUnits
@ -8,7 +10,7 @@ from aiohomekit.model.services import ServicesTypes
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .common import Helper, get_next_aid, setup_test_component from .common import Helper, setup_test_component
def create_service_with_ecobee_mode(accessory: Accessory): def create_service_with_ecobee_mode(accessory: Accessory):
@ -35,7 +37,9 @@ def create_service_with_temperature_units(accessory: Accessory):
async def test_migrate_unique_id( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test we can migrate a select unique id.""" """Test we can migrate a select unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -46,7 +50,7 @@ async def test_migrate_unique_id(
suggested_object_id="testdevice_current_mode", suggested_object_id="testdevice_current_mode",
) )
await setup_test_component(hass, create_service_with_ecobee_mode) await setup_test_component(hass, aid, create_service_with_ecobee_mode)
assert ( assert (
entity_registry.async_get(select.entity_id).unique_id entity_registry.async_get(select.entity_id).unique_id
@ -54,9 +58,13 @@ async def test_migrate_unique_id(
) )
async def test_read_current_mode(hass: HomeAssistant) -> None: async def test_read_current_mode(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test that Ecobee mode can be correctly read and show as human readable text.""" """Test that Ecobee mode can be correctly read and show as human readable text."""
helper = await setup_test_component(hass, create_service_with_ecobee_mode) helper = await setup_test_component(
hass, get_next_aid(), create_service_with_ecobee_mode
)
# Helper will be for the primary entity, which is the service. Make a helper for the sensor. # Helper will be for the primary entity, which is the service. Make a helper for the sensor.
ecobee_mode = Helper( ecobee_mode = Helper(
@ -92,9 +100,13 @@ async def test_read_current_mode(hass: HomeAssistant) -> None:
assert state.state == "away" assert state.state == "away"
async def test_write_current_mode(hass: HomeAssistant) -> None: async def test_write_current_mode(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test can set a specific mode.""" """Test can set a specific mode."""
helper = await setup_test_component(hass, create_service_with_ecobee_mode) helper = await setup_test_component(
hass, get_next_aid(), create_service_with_ecobee_mode
)
helper.accessory.services.first(service_type=ServicesTypes.THERMOSTAT) helper.accessory.services.first(service_type=ServicesTypes.THERMOSTAT)
# Helper will be for the primary entity, which is the service. Make a helper for the sensor. # Helper will be for the primary entity, which is the service. Make a helper for the sensor.
@ -140,9 +152,13 @@ async def test_write_current_mode(hass: HomeAssistant) -> None:
) )
async def test_read_select(hass: HomeAssistant) -> None: async def test_read_select(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test the generic select can read the current value.""" """Test the generic select can read the current value."""
helper = await setup_test_component(hass, create_service_with_temperature_units) helper = await setup_test_component(
hass, get_next_aid(), create_service_with_temperature_units
)
# Helper will be for the primary entity, which is the service. Make a helper for the sensor. # Helper will be for the primary entity, which is the service. Make a helper for the sensor.
select_entity = Helper( select_entity = Helper(
@ -170,9 +186,13 @@ async def test_read_select(hass: HomeAssistant) -> None:
assert state.state == "fahrenheit" assert state.state == "fahrenheit"
async def test_write_select(hass: HomeAssistant) -> None: async def test_write_select(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test can set a value.""" """Test can set a value."""
helper = await setup_test_component(hass, create_service_with_temperature_units) helper = await setup_test_component(
hass, get_next_aid(), create_service_with_temperature_units
)
helper.accessory.services.first(service_type=ServicesTypes.THERMOSTAT) helper.accessory.services.first(service_type=ServicesTypes.THERMOSTAT)
# Helper will be for the primary entity, which is the service. Make a helper for the sensor. # Helper will be for the primary entity, which is the service. Make a helper for the sensor.

View file

@ -1,5 +1,6 @@
"""Basic checks for HomeKit sensor.""" """Basic checks for HomeKit sensor."""
from collections.abc import Callable
from unittest.mock import patch from unittest.mock import patch
from aiohomekit.model import Transport from aiohomekit.model import Transport
@ -71,10 +72,12 @@ def create_battery_level_sensor(accessory):
return service return service
async def test_temperature_sensor_read_state(hass: HomeAssistant) -> None: async def test_temperature_sensor_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test reading the state of a HomeKit temperature sensor accessory.""" """Test reading the state of a HomeKit temperature sensor accessory."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_temperature_sensor_service, suffix="temperature" hass, get_next_aid(), create_temperature_sensor_service, suffix="temperature"
) )
state = await helper.async_update( state = await helper.async_update(
@ -97,10 +100,12 @@ async def test_temperature_sensor_read_state(hass: HomeAssistant) -> None:
assert state.attributes["state_class"] == SensorStateClass.MEASUREMENT assert state.attributes["state_class"] == SensorStateClass.MEASUREMENT
async def test_temperature_sensor_not_added_twice(hass: HomeAssistant) -> None: async def test_temperature_sensor_not_added_twice(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""A standalone temperature sensor should not get a characteristic AND a service entity.""" """A standalone temperature sensor should not get a characteristic AND a service entity."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_temperature_sensor_service, suffix="temperature" hass, get_next_aid(), create_temperature_sensor_service, suffix="temperature"
) )
created_sensors = set() created_sensors = set()
@ -111,10 +116,12 @@ async def test_temperature_sensor_not_added_twice(hass: HomeAssistant) -> None:
assert created_sensors == {helper.entity_id} assert created_sensors == {helper.entity_id}
async def test_humidity_sensor_read_state(hass: HomeAssistant) -> None: async def test_humidity_sensor_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test reading the state of a HomeKit humidity sensor accessory.""" """Test reading the state of a HomeKit humidity sensor accessory."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_humidity_sensor_service, suffix="humidity" hass, get_next_aid(), create_humidity_sensor_service, suffix="humidity"
) )
state = await helper.async_update( state = await helper.async_update(
@ -136,10 +143,12 @@ async def test_humidity_sensor_read_state(hass: HomeAssistant) -> None:
assert state.attributes["device_class"] == SensorDeviceClass.HUMIDITY assert state.attributes["device_class"] == SensorDeviceClass.HUMIDITY
async def test_light_level_sensor_read_state(hass: HomeAssistant) -> None: async def test_light_level_sensor_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test reading the state of a HomeKit temperature sensor accessory.""" """Test reading the state of a HomeKit temperature sensor accessory."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_light_level_sensor_service, suffix="light_level" hass, get_next_aid(), create_light_level_sensor_service, suffix="light_level"
) )
state = await helper.async_update( state = await helper.async_update(
@ -161,10 +170,15 @@ async def test_light_level_sensor_read_state(hass: HomeAssistant) -> None:
assert state.attributes["device_class"] == SensorDeviceClass.ILLUMINANCE assert state.attributes["device_class"] == SensorDeviceClass.ILLUMINANCE
async def test_carbon_dioxide_level_sensor_read_state(hass: HomeAssistant) -> None: async def test_carbon_dioxide_level_sensor_read_state(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test reading the state of a HomeKit carbon dioxide sensor accessory.""" """Test reading the state of a HomeKit carbon dioxide sensor accessory."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_carbon_dioxide_level_sensor_service, suffix="carbon_dioxide" hass,
get_next_aid(),
create_carbon_dioxide_level_sensor_service,
suffix="carbon_dioxide",
) )
state = await helper.async_update( state = await helper.async_update(
@ -184,10 +198,12 @@ async def test_carbon_dioxide_level_sensor_read_state(hass: HomeAssistant) -> No
assert state.state == "20" assert state.state == "20"
async def test_battery_level_sensor(hass: HomeAssistant) -> None: async def test_battery_level_sensor(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test reading the state of a HomeKit battery level sensor.""" """Test reading the state of a HomeKit battery level sensor."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_battery_level_sensor, suffix="battery" hass, get_next_aid(), create_battery_level_sensor, suffix="battery"
) )
state = await helper.async_update( state = await helper.async_update(
@ -211,10 +227,12 @@ async def test_battery_level_sensor(hass: HomeAssistant) -> None:
assert state.attributes["device_class"] == SensorDeviceClass.BATTERY assert state.attributes["device_class"] == SensorDeviceClass.BATTERY
async def test_battery_charging(hass: HomeAssistant) -> None: async def test_battery_charging(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test reading the state of a HomeKit battery's charging state.""" """Test reading the state of a HomeKit battery's charging state."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_battery_level_sensor, suffix="battery" hass, get_next_aid(), create_battery_level_sensor, suffix="battery"
) )
state = await helper.async_update( state = await helper.async_update(
@ -235,10 +253,12 @@ async def test_battery_charging(hass: HomeAssistant) -> None:
assert state.attributes["icon"] == "mdi:battery-charging-20" assert state.attributes["icon"] == "mdi:battery-charging-20"
async def test_battery_low(hass: HomeAssistant) -> None: async def test_battery_low(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test reading the state of a HomeKit battery's low state.""" """Test reading the state of a HomeKit battery's low state."""
helper = await setup_test_component( helper = await setup_test_component(
hass, create_battery_level_sensor, suffix="battery" hass, get_next_aid(), create_battery_level_sensor, suffix="battery"
) )
state = await helper.async_update( state = await helper.async_update(
@ -277,9 +297,11 @@ def create_switch_with_sensor(accessory):
return service return service
async def test_switch_with_sensor(hass: HomeAssistant) -> None: async def test_switch_with_sensor(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test a switch service that has a sensor characteristic is correctly handled.""" """Test a switch service that has a sensor characteristic is correctly handled."""
helper = await setup_test_component(hass, create_switch_with_sensor) helper = await setup_test_component(hass, get_next_aid(), create_switch_with_sensor)
# Helper will be for the primary entity, which is the outlet. Make a helper for the sensor. # Helper will be for the primary entity, which is the outlet. Make a helper for the sensor.
energy_helper = Helper( energy_helper = Helper(
@ -307,9 +329,11 @@ async def test_switch_with_sensor(hass: HomeAssistant) -> None:
assert state.state == "50" assert state.state == "50"
async def test_sensor_unavailable(hass: HomeAssistant) -> None: async def test_sensor_unavailable(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test a sensor becoming unavailable.""" """Test a sensor becoming unavailable."""
helper = await setup_test_component(hass, create_switch_with_sensor) helper = await setup_test_component(hass, get_next_aid(), create_switch_with_sensor)
outlet = helper.accessory.services.first(service_type=ServicesTypes.OUTLET) outlet = helper.accessory.services.first(service_type=ServicesTypes.OUTLET)
on_char = outlet[CharacteristicsTypes.ON] on_char = outlet[CharacteristicsTypes.ON]
@ -383,7 +407,9 @@ def test_thread_status_to_str() -> None:
@pytest.mark.usefixtures("enable_bluetooth", "entity_registry_enabled_by_default") @pytest.mark.usefixtures("enable_bluetooth", "entity_registry_enabled_by_default")
async def test_rssi_sensor(hass: HomeAssistant) -> None: async def test_rssi_sensor(
hass: HomeAssistant, get_next_aid: Callable[[], int]
) -> None:
"""Test an rssi sensor.""" """Test an rssi sensor."""
inject_bluetooth_service_info(hass, TEST_DEVICE_SERVICE_INFO) inject_bluetooth_service_info(hass, TEST_DEVICE_SERVICE_INFO)
@ -398,14 +424,20 @@ async def test_rssi_sensor(hass: HomeAssistant) -> None:
# Any accessory will do for this test, but we need at least # Any accessory will do for this test, but we need at least
# one or the rssi sensor will not be created # one or the rssi sensor will not be created
await setup_test_component( await setup_test_component(
hass, create_battery_level_sensor, suffix="battery", connection="BLE" hass,
get_next_aid(),
create_battery_level_sensor,
suffix="battery",
connection="BLE",
) )
assert hass.states.get("sensor.testdevice_signal_strength").state == "-56" assert hass.states.get("sensor.testdevice_signal_strength").state == "-56"
@pytest.mark.usefixtures("enable_bluetooth", "entity_registry_enabled_by_default") @pytest.mark.usefixtures("enable_bluetooth", "entity_registry_enabled_by_default")
async def test_migrate_rssi_sensor_unique_id( async def test_migrate_rssi_sensor_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test an rssi sensor unique id migration.""" """Test an rssi sensor unique id migration."""
rssi_sensor = entity_registry.async_get_or_create( rssi_sensor = entity_registry.async_get_or_create(
@ -428,7 +460,11 @@ async def test_migrate_rssi_sensor_unique_id(
# Any accessory will do for this test, but we need at least # Any accessory will do for this test, but we need at least
# one or the rssi sensor will not be created # one or the rssi sensor will not be created
await setup_test_component( await setup_test_component(
hass, create_battery_level_sensor, suffix="battery", connection="BLE" hass,
get_next_aid(),
create_battery_level_sensor,
suffix="battery",
connection="BLE",
) )
assert hass.states.get("sensor.renamed_rssi").state == "-56" assert hass.states.get("sensor.renamed_rssi").state == "-56"

View file

@ -1,5 +1,6 @@
"""Basic checks for entity map storage.""" """Basic checks for entity map storage."""
from collections.abc import Callable
from typing import Any from typing import Any
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import CharacteristicsTypes
@ -72,10 +73,10 @@ def create_lightbulb_service(accessory):
async def test_storage_is_updated_on_add( async def test_storage_is_updated_on_add(
hass: HomeAssistant, hass_storage: dict[str, Any] hass: HomeAssistant, hass_storage: dict[str, Any], get_next_aid: Callable[[], int]
) -> None: ) -> None:
"""Test entity map storage is cleaned up on adding an accessory.""" """Test entity map storage is cleaned up on adding an accessory."""
await setup_test_component(hass, create_lightbulb_service) await setup_test_component(hass, get_next_aid(), create_lightbulb_service)
entity_map: EntityMapStorage = hass.data[ENTITY_MAP] entity_map: EntityMapStorage = hass.data[ENTITY_MAP]
hkid = "00:00:00:00:00:00" hkid = "00:00:00:00:00:00"

View file

@ -1,5 +1,7 @@
"""Basic checks for HomeKitSwitch.""" """Basic checks for HomeKitSwitch."""
from collections.abc import Callable
from aiohomekit.model.characteristics import ( from aiohomekit.model.characteristics import (
CharacteristicsTypes, CharacteristicsTypes,
InUseValues, InUseValues,
@ -10,7 +12,7 @@ from aiohomekit.model.services import ServicesTypes
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er 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): def create_switch_service(accessory):
@ -50,9 +52,11 @@ def create_char_switch_service(accessory):
on_char.value = False 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.""" """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( await hass.services.async_call(
"switch", "turn_on", {"entity_id": "switch.testdevice"}, blocking=True "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.""" """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 # Initial state is that the switch is off and the outlet isn't in use
switch_1 = await helper.poll_and_get_state() 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 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.""" """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( await hass.services.async_call(
"switch", "turn_on", {"entity_id": "switch.testdevice"}, blocking=True "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.""" """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 # Initial state is that the switch is off and the outlet isn't in use
switch_1 = await helper.poll_and_get_state() 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 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.""" """Test that we can turn a characteristic on and off again."""
helper = await setup_test_component( 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( 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.""" """Test that we can read the state of a HomeKit characteristic switch."""
helper = await setup_test_component( 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 # 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( async def test_migrate_unique_id(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant,
entity_registry: er.EntityRegistry,
get_next_aid: Callable[[], int],
) -> None: ) -> None:
"""Test a we can migrate a switch unique id.""" """Test a we can migrate a switch unique id."""
aid = get_next_aid() aid = get_next_aid()
@ -235,7 +251,9 @@ async def test_migrate_unique_id(
"homekit_controller", "homekit_controller",
f"homekit-0001-aid:{aid}-sid:8-cid:9", 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 ( assert (
entity_registry.async_get(switch_entry.entity_id).unique_id entity_registry.async_get(switch_entry.entity_id).unique_id