Refactor homekit_controller to be fully asynchronous (#32111)

* Port homekit_controller to aiohomekit

* Remove succeed() test helper

* Remove fail() test helper
This commit is contained in:
Jc2k 2020-02-24 09:55:33 +00:00 committed by GitHub
parent a1a835cf54
commit df9363610c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 560 additions and 583 deletions

View file

@ -1,5 +1,8 @@
"""Basic checks for HomeKit motion sensors and contact sensors."""
from tests.components.homekit_controller.common import FakeService, setup_test_component
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes
from tests.components.homekit_controller.common import setup_test_component
V1_ON = ("fan", "on")
V1_ROTATION_DIRECTION = ("fan", "rotation.direction")
@ -11,50 +14,45 @@ V2_ROTATION_SPEED = ("fanv2", "rotation.speed")
V2_SWING_MODE = ("fanv2", "swing-mode")
def create_fan_service():
def create_fan_service(accessory):
"""
Define fan v1 characteristics as per HAP spec.
This service is no longer documented in R2 of the public HAP spec but existing
devices out there use it (like the SIMPLEconnect fan)
"""
service = FakeService("public.hap.service.fan")
service = accessory.add_service(ServicesTypes.FAN)
cur_state = service.add_characteristic("on")
cur_state = service.add_char(CharacteristicsTypes.ON)
cur_state.value = 0
cur_state = service.add_characteristic("rotation.direction")
cur_state.value = 0
direction = service.add_char(CharacteristicsTypes.ROTATION_DIRECTION)
direction.value = 0
cur_state = service.add_characteristic("rotation.speed")
cur_state.value = 0
return service
speed = service.add_char(CharacteristicsTypes.ROTATION_SPEED)
speed.value = 0
def create_fanv2_service():
def create_fanv2_service(accessory):
"""Define fan v2 characteristics as per HAP spec."""
service = FakeService("public.hap.service.fanv2")
service = accessory.add_service(ServicesTypes.FAN_V2)
cur_state = service.add_characteristic("active")
cur_state = service.add_char(CharacteristicsTypes.ACTIVE)
cur_state.value = 0
cur_state = service.add_characteristic("rotation.direction")
cur_state.value = 0
direction = service.add_char(CharacteristicsTypes.ROTATION_DIRECTION)
direction.value = 0
cur_state = service.add_characteristic("rotation.speed")
cur_state.value = 0
speed = service.add_char(CharacteristicsTypes.ROTATION_SPEED)
speed.value = 0
cur_state = service.add_characteristic("swing-mode")
cur_state.value = 0
return service
swing_mode = service.add_char(CharacteristicsTypes.SWING_MODE)
swing_mode.value = 0
async def test_fan_read_state(hass, utcnow):
"""Test that we can read the state of a HomeKit fan accessory."""
sensor = create_fan_service()
helper = await setup_test_component(hass, [sensor])
helper = await setup_test_component(hass, create_fan_service)
helper.characteristics[V1_ON].value = False
state = await helper.poll_and_get_state()
@ -67,8 +65,7 @@ async def test_fan_read_state(hass, utcnow):
async def test_turn_on(hass, utcnow):
"""Test that we can turn a fan on."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fan_service)
await hass.services.async_call(
"fan",
@ -100,8 +97,7 @@ async def test_turn_on(hass, utcnow):
async def test_turn_off(hass, utcnow):
"""Test that we can turn a fan off."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fan_service)
helper.characteristics[V1_ON].value = 1
@ -113,8 +109,7 @@ async def test_turn_off(hass, utcnow):
async def test_set_speed(hass, utcnow):
"""Test that we set fan speed."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fan_service)
helper.characteristics[V1_ON].value = 1
@ -153,8 +148,7 @@ async def test_set_speed(hass, utcnow):
async def test_speed_read(hass, utcnow):
"""Test that we can read a fans oscillation."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fan_service)
helper.characteristics[V1_ON].value = 1
helper.characteristics[V1_ROTATION_SPEED].value = 100
@ -177,8 +171,7 @@ async def test_speed_read(hass, utcnow):
async def test_set_direction(hass, utcnow):
"""Test that we can set fan spin direction."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fan_service)
await hass.services.async_call(
"fan",
@ -199,8 +192,7 @@ async def test_set_direction(hass, utcnow):
async def test_direction_read(hass, utcnow):
"""Test that we can read a fans oscillation."""
fan = create_fan_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fan_service)
helper.characteristics[V1_ROTATION_DIRECTION].value = 0
state = await helper.poll_and_get_state()
@ -213,8 +205,7 @@ async def test_direction_read(hass, utcnow):
async def test_fanv2_read_state(hass, utcnow):
"""Test that we can read the state of a HomeKit fan accessory."""
sensor = create_fanv2_service()
helper = await setup_test_component(hass, [sensor])
helper = await setup_test_component(hass, create_fanv2_service)
helper.characteristics[V2_ACTIVE].value = False
state = await helper.poll_and_get_state()
@ -227,8 +218,7 @@ async def test_fanv2_read_state(hass, utcnow):
async def test_v2_turn_on(hass, utcnow):
"""Test that we can turn a fan on."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fanv2_service)
await hass.services.async_call(
"fan",
@ -260,8 +250,7 @@ async def test_v2_turn_on(hass, utcnow):
async def test_v2_turn_off(hass, utcnow):
"""Test that we can turn a fan off."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fanv2_service)
helper.characteristics[V2_ACTIVE].value = 1
@ -273,8 +262,7 @@ async def test_v2_turn_off(hass, utcnow):
async def test_v2_set_speed(hass, utcnow):
"""Test that we set fan speed."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fanv2_service)
helper.characteristics[V2_ACTIVE].value = 1
@ -313,8 +301,7 @@ async def test_v2_set_speed(hass, utcnow):
async def test_v2_speed_read(hass, utcnow):
"""Test that we can read a fans oscillation."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fanv2_service)
helper.characteristics[V2_ACTIVE].value = 1
helper.characteristics[V2_ROTATION_SPEED].value = 100
@ -337,8 +324,7 @@ async def test_v2_speed_read(hass, utcnow):
async def test_v2_set_direction(hass, utcnow):
"""Test that we can set fan spin direction."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fanv2_service)
await hass.services.async_call(
"fan",
@ -359,8 +345,7 @@ async def test_v2_set_direction(hass, utcnow):
async def test_v2_direction_read(hass, utcnow):
"""Test that we can read a fans oscillation."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fanv2_service)
helper.characteristics[V2_ROTATION_DIRECTION].value = 0
state = await helper.poll_and_get_state()
@ -373,8 +358,7 @@ async def test_v2_direction_read(hass, utcnow):
async def test_v2_oscillate(hass, utcnow):
"""Test that we can control a fans oscillation."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fanv2_service)
await hass.services.async_call(
"fan",
@ -395,8 +379,7 @@ async def test_v2_oscillate(hass, utcnow):
async def test_v2_oscillate_read(hass, utcnow):
"""Test that we can read a fans oscillation."""
fan = create_fanv2_service()
helper = await setup_test_component(hass, [fan])
helper = await setup_test_component(hass, create_fanv2_service)
helper.characteristics[V2_SWING_MODE].value = 0
state = await helper.poll_and_get_state()