Add some tests for Koogeek LS1 (#22141)
This commit is contained in:
parent
188293770e
commit
c2aa06d0d4
3 changed files with 390 additions and 15 deletions
|
@ -1,4 +1,5 @@
|
|||
"""Code to support homekit_controller tests."""
|
||||
import json
|
||||
from datetime import timedelta
|
||||
from unittest import mock
|
||||
|
||||
|
@ -147,6 +148,41 @@ class FakeService(AbstractService):
|
|||
return char
|
||||
|
||||
|
||||
def setup_accessories_from_file(path):
|
||||
"""Load an collection of accessory defs from JSON data."""
|
||||
with open(path, 'r') as accessories_data:
|
||||
accessories_json = json.load(accessories_data)
|
||||
|
||||
accessories = []
|
||||
|
||||
for accessory_data in accessories_json:
|
||||
accessory = Accessory('Name', 'Mfr', 'Model', '0001', '0.1')
|
||||
accessory.services = []
|
||||
accessory.aid = accessory_data['aid']
|
||||
for service_data in accessory_data['services']:
|
||||
service = FakeService('public.hap.service.accessory-information')
|
||||
service.type = service_data['type']
|
||||
service.iid = service_data['iid']
|
||||
|
||||
for char_data in service_data['characteristics']:
|
||||
char = FakeCharacteristic(1, '23', None)
|
||||
char.type = char_data['type']
|
||||
char.iid = char_data['iid']
|
||||
char.perms = char_data['perms']
|
||||
char.format = char_data['format']
|
||||
if 'description' in char_data:
|
||||
char.description = char_data['description']
|
||||
if 'value' in char_data:
|
||||
char.value = char_data['value']
|
||||
service.characteristics.append(char)
|
||||
|
||||
accessory.services.append(service)
|
||||
|
||||
accessories.append(accessory)
|
||||
|
||||
return accessories
|
||||
|
||||
|
||||
async def setup_platform(hass):
|
||||
"""Load the platform but with a fake Controller API."""
|
||||
config = {
|
||||
|
@ -161,6 +197,30 @@ async def setup_platform(hass):
|
|||
return fake_controller
|
||||
|
||||
|
||||
async def setup_test_accessories(hass, accessories, capitalize=False):
|
||||
"""Load a fake homekit accessory based on a homekit accessory model.
|
||||
|
||||
If capitalize is True, property names will be in upper case.
|
||||
"""
|
||||
fake_controller = await setup_platform(hass)
|
||||
pairing = fake_controller.add(accessories)
|
||||
|
||||
discovery_info = {
|
||||
'host': '127.0.0.1',
|
||||
'port': 8080,
|
||||
'properties': {
|
||||
('MD' if capitalize else 'md'): 'TestDevice',
|
||||
('ID' if capitalize else 'id'): '00:00:00:00:00:00',
|
||||
('C#' if capitalize else 'c#'): 1,
|
||||
}
|
||||
}
|
||||
|
||||
fire_service_discovered(hass, SERVICE_HOMEKIT, discovery_info)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return pairing
|
||||
|
||||
|
||||
async def setup_test_component(hass, services, capitalize=False, suffix=None):
|
||||
"""Load a fake homekit accessory based on a homekit accessory model.
|
||||
|
||||
|
@ -177,24 +237,10 @@ async def setup_test_component(hass, services, capitalize=False, suffix=None):
|
|||
|
||||
assert domain, 'Cannot map test homekit services to homeassistant domain'
|
||||
|
||||
fake_controller = await setup_platform(hass)
|
||||
|
||||
accessory = Accessory('TestDevice', 'example.com', 'Test', '0001', '0.1')
|
||||
accessory.services.extend(services)
|
||||
pairing = fake_controller.add([accessory])
|
||||
|
||||
discovery_info = {
|
||||
'host': '127.0.0.1',
|
||||
'port': 8080,
|
||||
'properties': {
|
||||
('MD' if capitalize else 'md'): 'TestDevice',
|
||||
('ID' if capitalize else 'id'): '00:00:00:00:00:00',
|
||||
('C#' if capitalize else 'c#'): 1,
|
||||
}
|
||||
}
|
||||
|
||||
fire_service_discovered(hass, SERVICE_HOMEKIT, discovery_info)
|
||||
await hass.async_block_till_done()
|
||||
pairing = await setup_test_accessories(hass, [accessory], capitalize)
|
||||
|
||||
entity = 'testdevice' if suffix is None else 'testdevice_{}'.format(suffix)
|
||||
return Helper(hass, '.'.join((domain, entity)), pairing, accessory)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue