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-22 16:22:44 +00:00
|
|
|
from homeassistant.components.homekit_controller.const import KNOWN_DEVICES
|
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
from tests.components.homekit_controller.common import setup_test_component
|
2019-01-28 12:20:32 +00:00
|
|
|
|
2020-02-26 18:35:53 +00:00
|
|
|
LIGHT_BULB_NAME = "Light Bulb"
|
|
|
|
LIGHT_BULB_ENTITY_ID = "light.testdevice"
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
LIGHT_ON = ("lightbulb", "on")
|
|
|
|
LIGHT_BRIGHTNESS = ("lightbulb", "brightness")
|
|
|
|
LIGHT_HUE = ("lightbulb", "hue")
|
|
|
|
LIGHT_SATURATION = ("lightbulb", "saturation")
|
|
|
|
LIGHT_COLOR_TEMP = ("lightbulb", "color-temperature")
|
2019-01-28 12:20:32 +00:00
|
|
|
|
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
def create_lightbulb_service(accessory):
|
2019-01-28 12:20:32 +00:00
|
|
|
"""Define lightbulb characteristics."""
|
2020-02-26 18:35:53 +00:00
|
|
|
service = accessory.add_service(ServicesTypes.LIGHTBULB, name=LIGHT_BULB_NAME)
|
2019-01-28 12:20:32 +00:00
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
on_char = service.add_char(CharacteristicsTypes.ON)
|
2019-01-28 12:20:32 +00:00
|
|
|
on_char.value = 0
|
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
brightness = service.add_char(CharacteristicsTypes.BRIGHTNESS)
|
2019-01-28 12:20:32 +00:00
|
|
|
brightness.value = 0
|
|
|
|
|
|
|
|
return service
|
|
|
|
|
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
def create_lightbulb_service_with_hs(accessory):
|
2019-01-28 12:20:32 +00:00
|
|
|
"""Define a lightbulb service with hue + saturation."""
|
2020-02-24 09:55:33 +00:00
|
|
|
service = create_lightbulb_service(accessory)
|
2019-01-28 12:20:32 +00:00
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
hue = service.add_char(CharacteristicsTypes.HUE)
|
2019-01-28 12:20:32 +00:00
|
|
|
hue.value = 0
|
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
saturation = service.add_char(CharacteristicsTypes.SATURATION)
|
2019-01-28 12:20:32 +00:00
|
|
|
saturation.value = 0
|
|
|
|
|
|
|
|
return service
|
|
|
|
|
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
def create_lightbulb_service_with_color_temp(accessory):
|
2019-01-28 12:20:32 +00:00
|
|
|
"""Define a lightbulb service with color temp."""
|
2020-02-24 09:55:33 +00:00
|
|
|
service = create_lightbulb_service(accessory)
|
2019-01-28 12:20:32 +00:00
|
|
|
|
2020-02-24 09:55:33 +00:00
|
|
|
color_temp = service.add_char(CharacteristicsTypes.COLOR_TEMPERATURE)
|
2019-01-28 12:20:32 +00:00
|
|
|
color_temp.value = 0
|
|
|
|
|
|
|
|
return service
|
2019-01-27 11:34:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_switch_change_light_state(hass, utcnow):
|
|
|
|
"""Test that we can turn a HomeKit light on and off again."""
|
2020-02-24 09:55:33 +00:00
|
|
|
helper = await setup_test_component(hass, create_lightbulb_service_with_hs)
|
2019-01-27 11:34:49 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"entity_id": "light.testdevice", "brightness": 255, "hs_color": [4, 5]},
|
|
|
|
blocking=True,
|
|
|
|
)
|
2019-01-28 12:20:32 +00:00
|
|
|
|
|
|
|
assert helper.characteristics[LIGHT_ON].value == 1
|
|
|
|
assert helper.characteristics[LIGHT_BRIGHTNESS].value == 100
|
|
|
|
assert helper.characteristics[LIGHT_HUE].value == 4
|
|
|
|
assert helper.characteristics[LIGHT_SATURATION].value == 5
|
2019-01-27 11:34:49 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"light", "turn_off", {"entity_id": "light.testdevice"}, blocking=True
|
|
|
|
)
|
2019-01-28 12:20:32 +00:00
|
|
|
assert helper.characteristics[LIGHT_ON].value == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_switch_change_light_state_color_temp(hass, utcnow):
|
|
|
|
"""Test that we can turn change color_temp."""
|
2020-02-24 09:55:33 +00:00
|
|
|
helper = await setup_test_component(hass, create_lightbulb_service_with_color_temp)
|
2019-01-28 12:20:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"entity_id": "light.testdevice", "brightness": 255, "color_temp": 400},
|
|
|
|
blocking=True,
|
|
|
|
)
|
2019-01-28 12:20:32 +00:00
|
|
|
assert helper.characteristics[LIGHT_ON].value == 1
|
|
|
|
assert helper.characteristics[LIGHT_BRIGHTNESS].value == 100
|
|
|
|
assert helper.characteristics[LIGHT_COLOR_TEMP].value == 400
|
2019-01-27 11:34:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_switch_read_light_state(hass, utcnow):
|
|
|
|
"""Test that we can read the state of a HomeKit light accessory."""
|
2020-02-24 09:55:33 +00:00
|
|
|
helper = await setup_test_component(hass, create_lightbulb_service_with_hs)
|
2019-01-27 11:34:49 +00:00
|
|
|
|
|
|
|
# Initial state is that the light is off
|
|
|
|
state = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "off"
|
2019-01-27 11:34:49 +00:00
|
|
|
|
|
|
|
# Simulate that someone switched on the device in the real world not via HA
|
2019-01-28 12:20:32 +00:00
|
|
|
helper.characteristics[LIGHT_ON].set_value(True)
|
|
|
|
helper.characteristics[LIGHT_BRIGHTNESS].value = 100
|
|
|
|
helper.characteristics[LIGHT_HUE].value = 4
|
|
|
|
helper.characteristics[LIGHT_SATURATION].value = 5
|
2019-01-27 11:34:49 +00:00
|
|
|
state = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "on"
|
|
|
|
assert state.attributes["brightness"] == 255
|
|
|
|
assert state.attributes["hs_color"] == (4, 5)
|
2019-01-27 11:34:49 +00:00
|
|
|
|
|
|
|
# Simulate that device switched off in the real world not via HA
|
2019-01-28 12:20:32 +00:00
|
|
|
helper.characteristics[LIGHT_ON].set_value(False)
|
|
|
|
state = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "off"
|
2019-01-28 12:20:32 +00:00
|
|
|
|
|
|
|
|
2020-02-26 18:35:53 +00:00
|
|
|
async def test_switch_push_light_state(hass, utcnow):
|
|
|
|
"""Test that we can read the state of a HomeKit light accessory."""
|
|
|
|
helper = await setup_test_component(hass, create_lightbulb_service_with_hs)
|
|
|
|
|
|
|
|
# Initial state is that the light is off
|
|
|
|
state = hass.states.get(LIGHT_BULB_ENTITY_ID)
|
|
|
|
assert state.state == "off"
|
|
|
|
|
|
|
|
await helper.update_named_service(
|
|
|
|
LIGHT_BULB_NAME,
|
|
|
|
{
|
|
|
|
CharacteristicsTypes.ON: True,
|
|
|
|
CharacteristicsTypes.BRIGHTNESS: 100,
|
|
|
|
CharacteristicsTypes.HUE: 4,
|
|
|
|
CharacteristicsTypes.SATURATION: 5,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
state = hass.states.get(LIGHT_BULB_ENTITY_ID)
|
|
|
|
assert state.state == "on"
|
|
|
|
assert state.attributes["brightness"] == 255
|
|
|
|
assert state.attributes["hs_color"] == (4, 5)
|
|
|
|
|
|
|
|
# Simulate that device switched off in the real world not via HA
|
|
|
|
await helper.update_named_service(LIGHT_BULB_NAME, {CharacteristicsTypes.ON: False})
|
|
|
|
state = hass.states.get(LIGHT_BULB_ENTITY_ID)
|
|
|
|
assert state.state == "off"
|
|
|
|
|
|
|
|
|
2019-01-28 12:20:32 +00:00
|
|
|
async def test_switch_read_light_state_color_temp(hass, utcnow):
|
|
|
|
"""Test that we can read the color_temp of a light accessory."""
|
2020-02-24 09:55:33 +00:00
|
|
|
helper = await setup_test_component(hass, create_lightbulb_service_with_color_temp)
|
2019-01-28 12:20:32 +00:00
|
|
|
|
|
|
|
# Initial state is that the light is off
|
2019-01-27 11:34:49 +00:00
|
|
|
state = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "off"
|
2019-01-28 12:20:32 +00:00
|
|
|
|
|
|
|
# Simulate that someone switched on the device in the real world not via HA
|
|
|
|
helper.characteristics[LIGHT_ON].set_value(True)
|
|
|
|
helper.characteristics[LIGHT_BRIGHTNESS].value = 100
|
|
|
|
helper.characteristics[LIGHT_COLOR_TEMP].value = 400
|
|
|
|
|
|
|
|
state = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "on"
|
|
|
|
assert state.attributes["brightness"] == 255
|
|
|
|
assert state.attributes["color_temp"] == 400
|
2019-03-13 01:45:34 +00:00
|
|
|
|
|
|
|
|
2020-02-26 18:35:53 +00:00
|
|
|
async def test_switch_push_light_state_color_temp(hass, utcnow):
|
|
|
|
"""Test that we can read the state of a HomeKit light accessory."""
|
|
|
|
helper = await setup_test_component(hass, create_lightbulb_service_with_color_temp)
|
|
|
|
|
|
|
|
# Initial state is that the light is off
|
|
|
|
state = hass.states.get(LIGHT_BULB_ENTITY_ID)
|
|
|
|
assert state.state == "off"
|
|
|
|
|
|
|
|
await helper.update_named_service(
|
|
|
|
LIGHT_BULB_NAME,
|
|
|
|
{
|
|
|
|
CharacteristicsTypes.ON: True,
|
|
|
|
CharacteristicsTypes.BRIGHTNESS: 100,
|
|
|
|
CharacteristicsTypes.COLOR_TEMPERATURE: 400,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
state = hass.states.get(LIGHT_BULB_ENTITY_ID)
|
|
|
|
assert state.state == "on"
|
|
|
|
assert state.attributes["brightness"] == 255
|
|
|
|
assert state.attributes["color_temp"] == 400
|
|
|
|
|
|
|
|
|
2019-03-13 01:45:34 +00:00
|
|
|
async def test_light_becomes_unavailable_but_recovers(hass, utcnow):
|
|
|
|
"""Test transition to and from unavailable state."""
|
2020-02-24 09:55:33 +00:00
|
|
|
helper = await setup_test_component(hass, create_lightbulb_service_with_color_temp)
|
2019-03-13 01:45:34 +00:00
|
|
|
|
|
|
|
# Initial state is that the light is off
|
|
|
|
state = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "off"
|
2019-03-13 01:45:34 +00:00
|
|
|
|
|
|
|
# Test device goes offline
|
|
|
|
helper.pairing.available = False
|
|
|
|
state = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "unavailable"
|
2019-03-13 01:45:34 +00:00
|
|
|
|
|
|
|
# Simulate that someone switched on the device in the real world not via HA
|
|
|
|
helper.characteristics[LIGHT_ON].set_value(True)
|
|
|
|
helper.characteristics[LIGHT_BRIGHTNESS].value = 100
|
|
|
|
helper.characteristics[LIGHT_COLOR_TEMP].value = 400
|
|
|
|
helper.pairing.available = True
|
|
|
|
|
|
|
|
state = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "on"
|
|
|
|
assert state.attributes["brightness"] == 255
|
|
|
|
assert state.attributes["color_temp"] == 400
|
2019-07-22 16:22:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_light_unloaded(hass, utcnow):
|
|
|
|
"""Test entity and HKDevice are correctly unloaded."""
|
2020-02-24 09:55:33 +00:00
|
|
|
helper = await setup_test_component(hass, create_lightbulb_service_with_color_temp)
|
2019-07-22 16:22:44 +00:00
|
|
|
|
|
|
|
# Initial state is that the light is off
|
|
|
|
state = await helper.poll_and_get_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "off"
|
2019-07-22 16:22:44 +00:00
|
|
|
|
|
|
|
unload_result = await helper.config_entry.async_unload(hass)
|
|
|
|
assert unload_result is True
|
|
|
|
|
|
|
|
# Make sure entity is unloaded
|
|
|
|
assert hass.states.get(helper.entity_id) is None
|
|
|
|
|
|
|
|
# Make sure HKDevice is no longer set to poll this accessory
|
2019-07-31 19:25:30 +00:00
|
|
|
conn = hass.data[KNOWN_DEVICES]["00:00:00:00:00:00"]
|
2019-07-22 16:22:44 +00:00
|
|
|
assert not conn.pollable_characteristics
|