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_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), ) accessories_json = json.loads(accessories_fixture) 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'] 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) accessories.append(accessory) return accessories
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) ) accessories_json = json.loads(accessories_fixture) 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"] 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) accessories.append(accessory) return accessories
def create_proxy(accessories_and_characteristics): """ Create a proxy in front of a set of accessories, services and characteristics. This allows to follow the communication of a controller and an accessory (e.g. an iPhone and some HomeKit IP camera). :param accessories_and_characteristics: the accessory data as described in the spec on page 73 and following. This contains all the data that will be used to create the proxy. :return: a list of `Accessory` instances whose services and characteristics are replaced by proxied versions. That means characteristic's callback functions for getting and setting values relay those calls to the proxied characteristics. """ accessories = [] logging.info('%<------ creating proxy ------') for accessory in accessories_and_characteristics: proxy_accessory = Accessory( '', '', '', '', '', ) aid = accessory['aid'] proxy_accessory.aid = aid logging.info('accessory with aid=%s', aid) proxy_accessory.services = [] accessories.append(proxy_accessory) for service in accessory['services']: service_iid = service['iid'] service_type = service['type'] short_type = ServicesTypes.get_short(service_type) logging.info(' %i.%i: >%s< (%s)', aid, service_iid, short_type, service_type) proxy_service = ProxyService(service_iid, service_type) proxy_accessory.add_service(proxy_service) for characteristic in service['characteristics']: characteristic_iid = characteristic['iid'] characteristic_type = characteristic['type'] short_type = CharacteristicsTypes.get_short( characteristic_type) characteristic_format = characteristic['format'] characteristic_value = characteristic.get('value') characteristic_perms = characteristic['perms'] logging.info(' %i.%i: %s >%s< (%s) [%s] %s', aid, characteristic_iid, characteristic_value, short_type, characteristic_type, ','.join(characteristic_perms), characteristic_format) proxy_characteristic = ProxyCharacteristic( characteristic_iid, characteristic_type, characteristic_format) proxy_service.append_characteristic(proxy_characteristic) if characteristic_value: proxy_characteristic.value = characteristic_value proxy_characteristic.perms = characteristic_perms proxy_characteristic.set_set_value_callback( generate_set_value_callback(accessory['aid'], proxy_characteristic)) proxy_characteristic.set_get_value_callback( generate_get_value_callback(accessory['aid'], proxy_characteristic)) logging.info('%<------ finished creating proxy ------') return accessories