Improve homekit_controller tests (#65266)

This commit is contained in:
Jc2k 2022-01-30 22:59:39 +00:00 committed by GitHub
parent eb94fe1ca7
commit 58b8c30221
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 1487 additions and 650 deletions

View file

@ -43,6 +43,7 @@ def create_char_switch_service(accessory):
service = accessory.add_service(ServicesTypes.OUTLET)
on_char = service.add_char(CharacteristicsTypes.Vendor.AQARA_PAIRING_MODE)
on_char.perms.append("ev")
on_char.value = False
@ -53,12 +54,22 @@ async def test_switch_change_outlet_state(hass, utcnow):
await hass.services.async_call(
"switch", "turn_on", {"entity_id": "switch.testdevice"}, blocking=True
)
assert helper.characteristics[("outlet", "on")].value == 1
helper.async_assert_service_values(
ServicesTypes.OUTLET,
{
CharacteristicsTypes.ON: 1,
},
)
await hass.services.async_call(
"switch", "turn_off", {"entity_id": "switch.testdevice"}, blocking=True
)
assert helper.characteristics[("outlet", "on")].value == 0
helper.async_assert_service_values(
ServicesTypes.OUTLET,
{
CharacteristicsTypes.ON: 0,
},
)
async def test_switch_read_outlet_state(hass, utcnow):
@ -71,19 +82,25 @@ async def test_switch_read_outlet_state(hass, utcnow):
assert switch_1.attributes["outlet_in_use"] is False
# Simulate that someone switched on the device in the real world not via HA
helper.characteristics[("outlet", "on")].set_value(True)
switch_1 = await helper.poll_and_get_state()
switch_1 = await helper.async_update(
ServicesTypes.OUTLET,
{CharacteristicsTypes.ON: True},
)
assert switch_1.state == "on"
assert switch_1.attributes["outlet_in_use"] is False
# Simulate that device switched off in the real world not via HA
helper.characteristics[("outlet", "on")].set_value(False)
switch_1 = await helper.poll_and_get_state()
switch_1 = await helper.async_update(
ServicesTypes.OUTLET,
{CharacteristicsTypes.ON: False},
)
assert switch_1.state == "off"
# Simulate that someone plugged something into the device
helper.characteristics[("outlet", "outlet-in-use")].value = True
switch_1 = await helper.poll_and_get_state()
switch_1 = await helper.async_update(
ServicesTypes.OUTLET,
{CharacteristicsTypes.OUTLET_IN_USE: True},
)
assert switch_1.state == "off"
assert switch_1.attributes["outlet_in_use"] is True
@ -95,12 +112,22 @@ async def test_valve_change_active_state(hass, utcnow):
await hass.services.async_call(
"switch", "turn_on", {"entity_id": "switch.testdevice"}, blocking=True
)
assert helper.characteristics[("valve", "active")].value == 1
helper.async_assert_service_values(
ServicesTypes.VALVE,
{
CharacteristicsTypes.ACTIVE: 1,
},
)
await hass.services.async_call(
"switch", "turn_off", {"entity_id": "switch.testdevice"}, blocking=True
)
assert helper.characteristics[("valve", "active")].value == 0
helper.async_assert_service_values(
ServicesTypes.VALVE,
{
CharacteristicsTypes.ACTIVE: 0,
},
)
async def test_valve_read_state(hass, utcnow):
@ -115,20 +142,24 @@ async def test_valve_read_state(hass, utcnow):
assert switch_1.attributes["remaining_duration"] == 99
# Simulate that someone switched on the device in the real world not via HA
helper.characteristics[("valve", "active")].set_value(True)
switch_1 = await helper.poll_and_get_state()
switch_1 = await helper.async_update(
ServicesTypes.VALVE,
{CharacteristicsTypes.ACTIVE: True},
)
assert switch_1.state == "on"
# Simulate that someone configured the device in the real world not via HA
helper.characteristics[
("valve", "is-configured")
].value = IsConfiguredValues.NOT_CONFIGURED
switch_1 = await helper.poll_and_get_state()
switch_1 = await helper.async_update(
ServicesTypes.VALVE,
{CharacteristicsTypes.IS_CONFIGURED: IsConfiguredValues.NOT_CONFIGURED},
)
assert switch_1.attributes["is_configured"] is False
# Simulate that someone using the device in the real world not via HA
helper.characteristics[("valve", "in-use")].value = InUseValues.NOT_IN_USE
switch_1 = await helper.poll_and_get_state()
switch_1 = await helper.async_update(
ServicesTypes.VALVE,
{CharacteristicsTypes.IN_USE: InUseValues.NOT_IN_USE},
)
assert switch_1.attributes["in_use"] is False
@ -137,8 +168,6 @@ async def test_char_switch_change_state(hass, utcnow):
helper = await setup_test_component(
hass, create_char_switch_service, suffix="pairing_mode"
)
svc = helper.accessory.services.first(service_type=ServicesTypes.OUTLET)
pairing_mode = svc[CharacteristicsTypes.Vendor.AQARA_PAIRING_MODE]
await hass.services.async_call(
"switch",
@ -146,7 +175,12 @@ async def test_char_switch_change_state(hass, utcnow):
{"entity_id": "switch.testdevice_pairing_mode"},
blocking=True,
)
assert pairing_mode.value is True
helper.async_assert_service_values(
ServicesTypes.OUTLET,
{
CharacteristicsTypes.Vendor.AQARA_PAIRING_MODE: True,
},
)
await hass.services.async_call(
"switch",
@ -154,7 +188,12 @@ async def test_char_switch_change_state(hass, utcnow):
{"entity_id": "switch.testdevice_pairing_mode"},
blocking=True,
)
assert pairing_mode.value is False
helper.async_assert_service_values(
ServicesTypes.OUTLET,
{
CharacteristicsTypes.Vendor.AQARA_PAIRING_MODE: False,
},
)
async def test_char_switch_read_state(hass, utcnow):
@ -162,19 +201,17 @@ async def test_char_switch_read_state(hass, utcnow):
helper = await setup_test_component(
hass, create_char_switch_service, suffix="pairing_mode"
)
svc = helper.accessory.services.first(service_type=ServicesTypes.OUTLET)
pairing_mode = svc[CharacteristicsTypes.Vendor.AQARA_PAIRING_MODE]
# Initial state is that the switch is off
switch_1 = await helper.poll_and_get_state()
assert switch_1.state == "off"
# Simulate that someone switched on the device in the real world not via HA
pairing_mode.set_value(True)
switch_1 = await helper.poll_and_get_state()
switch_1 = await helper.async_update(
ServicesTypes.OUTLET,
{CharacteristicsTypes.Vendor.AQARA_PAIRING_MODE: True},
)
assert switch_1.state == "on"
# Simulate that device switched off in the real world not via HA
pairing_mode.set_value(False)
switch_1 = await helper.poll_and_get_state()
switch_1 = await helper.async_update(
ServicesTypes.OUTLET,
{CharacteristicsTypes.Vendor.AQARA_PAIRING_MODE: False},
)
assert switch_1.state == "off"