HomeKit Controller: Adopt config entries for pairing with homekit accessories (#23825)
* Fix user initiated pairing + show more user friendly name * Add lock around async_refresh_entity_map * Migrate homekit_controller to config entries. * Improve docstring Co-Authored-By: Martin Hjelmare <marhje52@kth.se> * Add dummy async_setup_platform * add_service -> async_add_service * Add missing returns * Enable coverage checks for homekit_controller
This commit is contained in:
parent
3508622e3b
commit
b8cbd39985
19 changed files with 334 additions and 287 deletions
|
@ -9,13 +9,15 @@ from homekit.model.characteristics import (
|
|||
AbstractCharacteristic, CharacteristicPermissions, CharacteristicsTypes)
|
||||
from homekit.model import Accessory, get_id
|
||||
from homekit.exceptions import AccessoryNotFoundError
|
||||
from homeassistant.components.homekit_controller import SERVICE_HOMEKIT
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.homekit_controller.const import (
|
||||
CONTROLLER, DOMAIN, HOMEKIT_ACCESSORY_DISPATCH)
|
||||
from homeassistant.components.homekit_controller import (
|
||||
async_setup_entry, config_flow)
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
from tests.common import (
|
||||
async_fire_time_changed, async_fire_service_discovered, load_fixture)
|
||||
from tests.common import async_fire_time_changed, load_fixture
|
||||
|
||||
|
||||
class FakePairing:
|
||||
|
@ -217,26 +219,36 @@ 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.
|
||||
"""
|
||||
async def setup_test_accessories(hass, accessories):
|
||||
"""Load a fake homekit device based on captured JSON profile."""
|
||||
fake_controller = await setup_platform(hass)
|
||||
pairing = fake_controller.add(accessories)
|
||||
|
||||
discovery_info = {
|
||||
'name': 'TestDevice',
|
||||
'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,
|
||||
'md': 'TestDevice',
|
||||
'id': '00:00:00:00:00:00',
|
||||
'c#': 1,
|
||||
}
|
||||
}
|
||||
|
||||
async_fire_service_discovered(hass, SERVICE_HOMEKIT, discovery_info)
|
||||
await hass.async_block_till_done()
|
||||
pairing.pairing_data.update({
|
||||
'AccessoryPairingID': discovery_info['properties']['id'],
|
||||
})
|
||||
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
1, 'homekit_controller', 'TestData', pairing.pairing_data,
|
||||
'test', config_entries.CONN_CLASS_LOCAL_PUSH
|
||||
)
|
||||
|
||||
pairing_cls_loc = 'homekit.controller.ip_implementation.IpPairing'
|
||||
with mock.patch(pairing_cls_loc) as pairing_cls:
|
||||
pairing_cls.return_value = pairing
|
||||
await async_setup_entry(hass, config_entry)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return pairing
|
||||
|
||||
|
@ -249,6 +261,7 @@ async def device_config_changed(hass, accessories):
|
|||
pairing.accessories = accessories
|
||||
|
||||
discovery_info = {
|
||||
'name': 'TestDevice',
|
||||
'host': '127.0.0.1',
|
||||
'port': 8080,
|
||||
'properties': {
|
||||
|
@ -259,7 +272,14 @@ async def device_config_changed(hass, accessories):
|
|||
}
|
||||
}
|
||||
|
||||
async_fire_service_discovered(hass, SERVICE_HOMEKIT, discovery_info)
|
||||
# Config Flow will abort and notify us if the discovery event is of
|
||||
# interest - in this case c# has incremented
|
||||
flow = config_flow.HomekitControllerFlowHandler()
|
||||
flow.hass = hass
|
||||
flow.context = {}
|
||||
result = await flow.async_step_discovery(discovery_info)
|
||||
assert result['type'] == 'abort'
|
||||
assert result['reason'] == 'already_configured'
|
||||
|
||||
# Wait for services to reconfigure
|
||||
await hass.async_block_till_done()
|
||||
|
@ -285,7 +305,6 @@ async def setup_test_component(hass, services, capitalize=False, suffix=None):
|
|||
accessory = Accessory('TestDevice', 'example.com', 'Test', '0001', '0.1')
|
||||
accessory.services.extend(services)
|
||||
|
||||
pairing = await setup_test_accessories(hass, [accessory], capitalize)
|
||||
|
||||
pairing = await setup_test_accessories(hass, [accessory])
|
||||
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