2019-01-27 11:34:49 +00:00
|
|
|
"""Basic checks for HomeKitSwitch."""
|
2020-02-24 09:55:33 +00:00
|
|
|
|
|
|
|
from aiohomekit.model.characteristics import CharacteristicsTypes
|
|
|
|
from aiohomekit.model.services import ServicesTypes
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from tests.components.homekit_controller.common import setup_test_component
|
2019-01-27 11:34:49 +00:00
|
|
|
|
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
def create_switch_service(accessory):
|
|
|
|
"""Define outlet characteristics."""
|
|
|
|
service = accessory.add_service(ServicesTypes.OUTLET)
|
|
|
|
|
|
|
|
on_char = service.add_char(CharacteristicsTypes.ON)
|
|
|
|
on_char.value = False
|
|
|
|
|
|
|
|
outlet_in_use = service.add_char(CharacteristicsTypes.OUTLET_IN_USE)
|
|
|
|
outlet_in_use.value = False
|
|
|
|
|
|
|
|
|
2019-01-27 11:34:49 +00:00
|
|
|
async def test_switch_change_outlet_state(hass, utcnow):
|
|
|
|
"""Test that we can turn a HomeKit outlet on and off again."""
|
2020-02-24 09:55:33 +00:00
|
|
|
helper = await setup_test_component(hass, create_switch_service)
|
2019-01-27 11:34:49 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"switch", "turn_on", {"entity_id": "switch.testdevice"}, blocking=True
|
|
|
|
)
|
|
|
|
assert helper.characteristics[("outlet", "on")].value == 1
|
2019-01-27 11:34:49 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"switch", "turn_off", {"entity_id": "switch.testdevice"}, blocking=True
|
|
|
|
)
|
|
|
|
assert helper.characteristics[("outlet", "on")].value == 0
|
2019-01-27 11:34:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_switch_read_outlet_state(hass, utcnow):
|
|
|
|
"""Test that we can read the state of a HomeKit outlet accessory."""
|
2020-02-24 09:55:33 +00:00
|
|
|
helper = await setup_test_component(hass, create_switch_service)
|
2019-01-27 11:34:49 +00:00
|
|
|
|
|
|
|
# Initial state is that the switch is off and the outlet isn't in use
|
|
|
|
switch_1 = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert switch_1.state == "off"
|
|
|
|
assert switch_1.attributes["outlet_in_use"] is False
|
2019-01-27 11:34:49 +00:00
|
|
|
|
|
|
|
# Simulate that someone switched on the device in the real world not via HA
|
2019-07-31 19:25:30 +00:00
|
|
|
helper.characteristics[("outlet", "on")].set_value(True)
|
2019-01-27 11:34:49 +00:00
|
|
|
switch_1 = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert switch_1.state == "on"
|
|
|
|
assert switch_1.attributes["outlet_in_use"] is False
|
2019-01-27 11:34:49 +00:00
|
|
|
|
|
|
|
# Simulate that device switched off in the real world not via HA
|
2019-07-31 19:25:30 +00:00
|
|
|
helper.characteristics[("outlet", "on")].set_value(False)
|
2019-01-27 11:34:49 +00:00
|
|
|
switch_1 = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert switch_1.state == "off"
|
2019-01-27 11:34:49 +00:00
|
|
|
|
|
|
|
# Simulate that someone plugged something into the device
|
2019-07-31 19:25:30 +00:00
|
|
|
helper.characteristics[("outlet", "outlet-in-use")].value = True
|
2019-01-27 11:34:49 +00:00
|
|
|
switch_1 = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert switch_1.state == "off"
|
|
|
|
assert switch_1.attributes["outlet_in_use"] is True
|