Black
This commit is contained in:
parent
da05dfe708
commit
4de97abc3a
2676 changed files with 163166 additions and 140084 deletions
|
@ -6,20 +6,23 @@ from unittest import mock
|
|||
|
||||
from homekit.model.services import AbstractService, ServicesTypes
|
||||
from homekit.model.characteristics import (
|
||||
AbstractCharacteristic, CharacteristicPermissions, CharacteristicsTypes)
|
||||
AbstractCharacteristic,
|
||||
CharacteristicPermissions,
|
||||
CharacteristicsTypes,
|
||||
)
|
||||
from homekit.model import Accessory, get_id
|
||||
from homekit.exceptions import AccessoryNotFoundError
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.homekit_controller.const import (
|
||||
CONTROLLER, DOMAIN, HOMEKIT_ACCESSORY_DISPATCH)
|
||||
from homeassistant.components.homekit_controller import (
|
||||
config_flow)
|
||||
CONTROLLER,
|
||||
DOMAIN,
|
||||
HOMEKIT_ACCESSORY_DISPATCH,
|
||||
)
|
||||
from homeassistant.components.homekit_controller import config_flow
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
from tests.common import (
|
||||
async_fire_time_changed, load_fixture, MockConfigEntry
|
||||
)
|
||||
from tests.common import async_fire_time_changed, load_fixture, MockConfigEntry
|
||||
|
||||
|
||||
class FakePairing:
|
||||
|
@ -38,17 +41,15 @@ class FakePairing:
|
|||
|
||||
def list_accessories_and_characteristics(self):
|
||||
"""Fake implementation of list_accessories_and_characteristics."""
|
||||
accessories = [
|
||||
a.to_accessory_and_service_list() for a in self.accessories
|
||||
]
|
||||
accessories = [a.to_accessory_and_service_list() for a in self.accessories]
|
||||
# replicate what happens upstream right now
|
||||
self.pairing_data['accessories'] = accessories
|
||||
self.pairing_data["accessories"] = accessories
|
||||
return accessories
|
||||
|
||||
def get_characteristics(self, characteristics):
|
||||
"""Fake implementation of get_characteristics."""
|
||||
if not self.available:
|
||||
raise AccessoryNotFoundError('Accessory not found')
|
||||
raise AccessoryNotFoundError("Accessory not found")
|
||||
|
||||
results = {}
|
||||
for aid, cid in characteristics:
|
||||
|
@ -59,9 +60,7 @@ class FakePairing:
|
|||
for char in service.characteristics:
|
||||
if char.iid != cid:
|
||||
continue
|
||||
results[(aid, cid)] = {
|
||||
'value': char.get_value()
|
||||
}
|
||||
results[(aid, cid)] = {"value": char.get_value()}
|
||||
return results
|
||||
|
||||
def put_characteristics(self, characteristics):
|
||||
|
@ -92,7 +91,7 @@ class FakeController:
|
|||
def add(self, accessories):
|
||||
"""Create and register a fake pairing for a simulated accessory."""
|
||||
pairing = FakePairing(accessories)
|
||||
self.pairings['00:00:00:00:00:00'] = pairing
|
||||
self.pairings["00:00:00:00:00:00"] = pairing
|
||||
return pairing
|
||||
|
||||
|
||||
|
@ -138,7 +137,7 @@ class FakeCharacteristic(AbstractCharacteristic):
|
|||
# is fixed.
|
||||
record = super().to_accessory_and_service_list()
|
||||
if self.valid_values:
|
||||
record['valid-values'] = self.valid_values
|
||||
record["valid-values"] = self.valid_values
|
||||
return record
|
||||
|
||||
|
||||
|
@ -152,11 +151,11 @@ class FakeService(AbstractService):
|
|||
|
||||
def add_characteristic(self, name):
|
||||
"""Add a characteristic to this service by name."""
|
||||
full_name = 'public.hap.characteristic.' + name
|
||||
full_name = "public.hap.characteristic." + name
|
||||
char = FakeCharacteristic(get_id(), full_name, None)
|
||||
char.perms = [
|
||||
CharacteristicPermissions.paired_read,
|
||||
CharacteristicPermissions.paired_write
|
||||
CharacteristicPermissions.paired_write,
|
||||
]
|
||||
self.characteristics.append(char)
|
||||
return char
|
||||
|
@ -172,38 +171,37 @@ async def time_changed(hass, seconds):
|
|||
async def setup_accessories_from_file(hass, path):
|
||||
"""Load an collection of accessory defs from JSON data."""
|
||||
accessories_fixture = await hass.async_add_executor_job(
|
||||
load_fixture,
|
||||
os.path.join('homekit_controller', path),
|
||||
load_fixture, os.path.join("homekit_controller", path)
|
||||
)
|
||||
accessories_json = json.loads(accessories_fixture)
|
||||
|
||||
accessories = []
|
||||
|
||||
for accessory_data in accessories_json:
|
||||
accessory = Accessory('Name', 'Mfr', 'Model', '0001', '0.1')
|
||||
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']
|
||||
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']
|
||||
if 'minValue' in char_data:
|
||||
char.minValue = char_data['minValue']
|
||||
if 'maxValue' in char_data:
|
||||
char.maxValue = char_data['maxValue']
|
||||
if 'valid-values' in char_data:
|
||||
char.valid_values = char_data['valid-values']
|
||||
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"]
|
||||
if "minValue" in char_data:
|
||||
char.minValue = char_data["minValue"]
|
||||
if "maxValue" in char_data:
|
||||
char.maxValue = char_data["maxValue"]
|
||||
if "valid-values" in char_data:
|
||||
char.valid_values = char_data["valid-values"]
|
||||
service.characteristics.append(char)
|
||||
|
||||
accessory.services.append(service)
|
||||
|
@ -215,12 +213,9 @@ async def setup_accessories_from_file(hass, path):
|
|||
|
||||
async def setup_platform(hass):
|
||||
"""Load the platform but with a fake Controller API."""
|
||||
config = {
|
||||
'discovery': {
|
||||
}
|
||||
}
|
||||
config = {"discovery": {}}
|
||||
|
||||
with mock.patch('homekit.Controller') as controller:
|
||||
with mock.patch("homekit.Controller") as controller:
|
||||
fake_controller = controller.return_value = FakeController()
|
||||
await async_setup_component(hass, DOMAIN, config)
|
||||
|
||||
|
@ -233,32 +228,28 @@ async def setup_test_accessories(hass, accessories):
|
|||
pairing = fake_controller.add(accessories)
|
||||
|
||||
discovery_info = {
|
||||
'name': 'TestDevice',
|
||||
'host': '127.0.0.1',
|
||||
'port': 8080,
|
||||
'properties': {
|
||||
'md': 'TestDevice',
|
||||
'id': '00:00:00:00:00:00',
|
||||
'c#': 1,
|
||||
}
|
||||
"name": "TestDevice",
|
||||
"host": "127.0.0.1",
|
||||
"port": 8080,
|
||||
"properties": {"md": "TestDevice", "id": "00:00:00:00:00:00", "c#": 1},
|
||||
}
|
||||
|
||||
pairing.pairing_data.update({
|
||||
'AccessoryPairingID': discovery_info['properties']['id'],
|
||||
})
|
||||
pairing.pairing_data.update(
|
||||
{"AccessoryPairingID": discovery_info["properties"]["id"]}
|
||||
)
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
version=1,
|
||||
domain='homekit_controller',
|
||||
entry_id='TestData',
|
||||
domain="homekit_controller",
|
||||
entry_id="TestData",
|
||||
data=pairing.pairing_data,
|
||||
title='test',
|
||||
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH
|
||||
title="test",
|
||||
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
|
||||
)
|
||||
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
pairing_cls_loc = 'homekit.controller.ip_implementation.IpPairing'
|
||||
pairing_cls_loc = "homekit.controller.ip_implementation.IpPairing"
|
||||
with mock.patch(pairing_cls_loc) as pairing_cls:
|
||||
pairing_cls.return_value = pairing
|
||||
await config_entry.async_setup(hass)
|
||||
|
@ -271,19 +262,19 @@ async def device_config_changed(hass, accessories):
|
|||
"""Discover new devices added to HomeAssistant at runtime."""
|
||||
# Update the accessories our FakePairing knows about
|
||||
controller = hass.data[CONTROLLER]
|
||||
pairing = controller.pairings['00:00:00:00:00:00']
|
||||
pairing = controller.pairings["00:00:00:00:00:00"]
|
||||
pairing.accessories = accessories
|
||||
|
||||
discovery_info = {
|
||||
'name': 'TestDevice',
|
||||
'host': '127.0.0.1',
|
||||
'port': 8080,
|
||||
'properties': {
|
||||
'md': 'TestDevice',
|
||||
'id': '00:00:00:00:00:00',
|
||||
'c#': '2',
|
||||
'sf': '0',
|
||||
}
|
||||
"name": "TestDevice",
|
||||
"host": "127.0.0.1",
|
||||
"port": 8080,
|
||||
"properties": {
|
||||
"md": "TestDevice",
|
||||
"id": "00:00:00:00:00:00",
|
||||
"c#": "2",
|
||||
"sf": "0",
|
||||
},
|
||||
}
|
||||
|
||||
# Config Flow will abort and notify us if the discovery event is of
|
||||
|
@ -292,8 +283,8 @@ async def device_config_changed(hass, accessories):
|
|||
flow.hass = hass
|
||||
flow.context = {}
|
||||
result = await flow.async_step_zeroconf(discovery_info)
|
||||
assert result['type'] == 'abort'
|
||||
assert result['reason'] == 'already_configured'
|
||||
assert result["type"] == "abort"
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
# Wait for services to reconfigure
|
||||
await hass.async_block_till_done()
|
||||
|
@ -314,13 +305,11 @@ async def setup_test_component(hass, services, capitalize=False, suffix=None):
|
|||
domain = HOMEKIT_ACCESSORY_DISPATCH[service_name]
|
||||
break
|
||||
|
||||
assert domain, 'Cannot map test homekit services to homeassistant domain'
|
||||
assert domain, "Cannot map test homekit services to homeassistant domain"
|
||||
|
||||
accessory = Accessory('TestDevice', 'example.com', 'Test', '0001', '0.1')
|
||||
accessory = Accessory("TestDevice", "example.com", "Test", "0001", "0.1")
|
||||
accessory.services.extend(services)
|
||||
|
||||
config_entry, 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, config_entry
|
||||
)
|
||||
entity = "testdevice" if suffix is None else "testdevice_{}".format(suffix)
|
||||
return Helper(hass, ".".join((domain, entity)), pairing, accessory, config_entry)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue