def setup(hass, config): """Setup zigate platform.""" port = config[DOMAIN].get(CONF_PORT) host = config[DOMAIN].get(CONF_HOST) gpio = config[DOMAIN].get('gpio', False) enable_led = config[DOMAIN].get('enable_led', True) polling = config[DOMAIN].get('polling', True) channel = config[DOMAIN].get('channel') scan_interval = datetime.timedelta( seconds=config[DOMAIN].get(CONF_SCAN_INTERVAL, SCAN_INTERVAL)) admin_panel = config[DOMAIN].get('admin_panel', True) persistent_file = os.path.join(hass.config.config_dir, 'zigate.json') _LOGGER.debug('Port : %s', port) _LOGGER.debug('Host : %s', host) _LOGGER.debug('GPIO : %s', gpio) _LOGGER.debug('Led : %s', enable_led) _LOGGER.debug('Channel : %s', channel) _LOGGER.debug('Scan interval : %s', scan_interval) myzigate = zigate.connect(port=port, host=host, path=persistent_file, auto_start=False, gpio=gpio) _LOGGER.debug('ZiGate object created %s', myzigate) hass.data[DOMAIN] = myzigate hass.data[DATA_ZIGATE_DEVICES] = {} hass.data[DATA_ZIGATE_ATTRS] = {} component = EntityComponent(_LOGGER, DOMAIN, hass, scan_interval) # component.setup(config) entity = ZiGateComponentEntity(myzigate) hass.data[DATA_ZIGATE_DEVICES]['zigate'] = entity component.add_entities([entity]) def device_added(**kwargs): device = kwargs['device'] _LOGGER.debug('Add device {}'.format(device)) ieee = device.ieee if ieee not in hass.data[DATA_ZIGATE_DEVICES]: hass.data[DATA_ZIGATE_DEVICES][ieee] = None # reserve entity = ZiGateDeviceEntity(hass, device, polling) hass.data[DATA_ZIGATE_DEVICES][ieee] = entity component.add_entities([entity]) if 'signal' in kwargs: hass.components.persistent_notification.create( ('A new ZiGate device "{}"' ' has been added !').format(device), title='ZiGate') def device_removed(**kwargs): # component.async_remove_entity device = kwargs['device'] ieee = device.ieee hass.components.persistent_notification.create( 'The ZiGate device {}({}) is gone.'.format(device.ieee, device.addr), title='ZiGate') entity = hass.data[DATA_ZIGATE_DEVICES][ieee] component.async_remove_entity(entity.entity_id) del hass.data[DATA_ZIGATE_DEVICES][ieee] def device_need_discovery(**kwargs): device = kwargs['device'] hass.components.persistent_notification.create( ('The ZiGate device {}({}) needs to be discovered' ' (missing important' ' information)').format(device.ieee, device.addr), title='ZiGate') zigate.dispatcher.connect(device_added, zigate.ZIGATE_DEVICE_ADDED, weak=False) zigate.dispatcher.connect(device_removed, zigate.ZIGATE_DEVICE_REMOVED, weak=False) zigate.dispatcher.connect(device_need_discovery, zigate.ZIGATE_DEVICE_NEED_DISCOVERY, weak=False) def attribute_updated(**kwargs): device = kwargs['device'] ieee = device.ieee attribute = kwargs['attribute'] _LOGGER.debug('Update attribute for device {} {}'.format( device, attribute)) entity = hass.data[DATA_ZIGATE_DEVICES].get(ieee) event_data = attribute.copy() if type(event_data.get('type')) == type: event_data['type'] = event_data['type'].__name__ event_data['ieee'] = device.ieee event_data['addr'] = device.addr event_data['device_type'] = device.get_property_value('type') if entity: event_data['entity_id'] = entity.entity_id hass.bus.fire('zigate.attribute_updated', event_data) zigate.dispatcher.connect(attribute_updated, zigate.ZIGATE_ATTRIBUTE_UPDATED, weak=False) def device_updated(**kwargs): device = kwargs['device'] _LOGGER.debug('Update device {}'.format(device)) ieee = device.ieee entity = hass.data[DATA_ZIGATE_DEVICES].get(ieee) if not entity: _LOGGER.debug('Device not found {}, adding it'.format(device)) device_added(device=device) event_data = {} event_data['ieee'] = device.ieee event_data['addr'] = device.addr event_data['device_type'] = device.get_property_value('type') if entity: event_data['entity_id'] = entity.entity_id hass.bus.fire('zigate.device_updated', event_data) zigate.dispatcher.connect(device_updated, zigate.ZIGATE_DEVICE_UPDATED, weak=False) zigate.dispatcher.connect(device_updated, zigate.ZIGATE_ATTRIBUTE_ADDED, weak=False) zigate.dispatcher.connect(device_updated, zigate.ZIGATE_DEVICE_ADDRESS_CHANGED, weak=False) def zigate_reset(service): myzigate.reset() def permit_join(service): myzigate.permit_join() def zigate_cleanup(service): ''' Remove missing device ''' myzigate.cleanup_devices() def start_zigate(service_event=None): myzigate.autoStart(channel) myzigate.start_auto_save() myzigate.set_led(enable_led) version = myzigate.get_version_text() if version < '3.1a': hass.components.persistent_notification.create( ('Your zigate firmware is outdated, ' 'Please upgrade to 3.1a or later !'), title='ZiGate') # first load for device in myzigate.devices: device_added(device=device) for platform in SUPPORTED_PLATFORMS: load_platform(hass, platform, DOMAIN, {}, config) hass.bus.fire('zigate.started') def stop_zigate(service=None): myzigate.save_state() myzigate.close() hass.bus.fire('zigate.stopped') def refresh_devices_list(service): myzigate.get_devices_list() def generate_templates(service): myzigate.generate_templates(hass.config.config_dir) def _get_addr_from_service_request(service): entity_id = service.data.get(ATTR_ENTITY_ID) ieee = service.data.get(IEEE) addr = service.data.get(ADDR) if entity_id: entity = component.get_entity(entity_id) if entity: addr = entity._device.addr elif ieee: device = myzigate.get_device_from_ieee(ieee) if device: addr = device.addr return addr def _to_int(value): ''' convert str to int ''' if 'x' in value: return int(value, 16) return int(value) def refresh_device(service): full = service.data.get('full', False) addr = _get_addr_from_service_request(service) if addr: myzigate.refresh_device(addr, full=full) else: for device in myzigate.devices: device.refresh_device(full=full) def discover_device(service): addr = _get_addr_from_service_request(service) if addr: myzigate.discover_device(addr, True) def network_scan(service): myzigate.start_network_scan() def raw_command(service): cmd = _to_int(service.data.get('cmd')) data = service.data.get('data', '') myzigate.send_data(cmd, data) def identify_device(service): addr = _get_addr_from_service_request(service) myzigate.identify_device(addr) def remove_device(service): addr = _get_addr_from_service_request(service) myzigate.remove_device(addr) def initiate_touchlink(service): myzigate.initiate_touchlink() def touchlink_factory_reset(service): myzigate.touchlink_factory_reset() def read_attribute(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint')) cluster = _to_int(service.data.get('cluster')) attribute_id = _to_int(service.data.get('attribute_id')) manufacturer_code = _to_int(service.data.get('manufacturer_code', '0')) myzigate.read_attribute_request(addr, endpoint, cluster, attribute_id, manufacturer_code=manufacturer_code) def write_attribute(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint')) cluster = _to_int(service.data.get('cluster')) attribute_id = _to_int(service.data.get('attribute_id')) attribute_type = _to_int(service.data.get('attribute_type')) value = _to_int(service.data.get('value')) attributes = [(attribute_id, attribute_type, value)] manufacturer_code = _to_int(service.data.get('manufacturer_code', '0')) myzigate.write_attribute_request(addr, endpoint, cluster, attributes, manufacturer_code=manufacturer_code) def add_group(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint')) groupaddr = service.data.get('group_addr') myzigate.add_group(addr, endpoint, groupaddr) def remove_group(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint')) groupaddr = service.data.get('group_addr') myzigate.remove_group(addr, endpoint, groupaddr) def get_group_membership(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint')) myzigate.get_group_membership(addr, endpoint) def action_onoff(service): addr = _get_addr_from_service_request(service) onoff = _to_int(service.data.get('onoff')) endpoint = _to_int(service.data.get('endpoint', '0')) ontime = _to_int(service.data.get('on_time', '0')) offtime = _to_int(service.data.get('off_time', '0')) effect = _to_int(service.data.get('effect', '0')) gradient = _to_int(service.data.get('gradient', '0')) myzigate.action_onoff(addr, endpoint, onoff, ontime, offtime, effect, gradient) def build_network_table(service): table = myzigate.build_neighbours_table( service.data.get('force', False)) _LOGGER.debug('Neighbours table {}'.format(table)) def ota_load_image(service): ota_image_path = service.data.get('imagepath') myzigate.ota_load_image(ota_image_path) def ota_image_notify(service): addr = _get_addr_from_service_request(service) destination_endpoint = _to_int( service.data.get('destination_endpoint', '1')) payload_type = _to_int(service.data.get('payload_type', '0')) myzigate.ota_image_notify(addr, destination_endpoint, payload_type) def get_ota_status(service): myzigate.get_ota_status() def view_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') scene = _to_int(service.data.get('scene')) myzigate.view_scene(addr, endpoint, groupaddr, scene) def add_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') scene = _to_int(service.data.get('scene')) name = service.data.get('scene_name') transition = _to_int(service.data.get('transition', '0')) myzigate.add_scene(addr, endpoint, groupaddr, scene, name, transition) def remove_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') scene = _to_int(service.data.get('scene', -1)) if scene == -1: scene = None myzigate.remove_scene(addr, endpoint, groupaddr, scene) def store_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') scene = _to_int(service.data.get('scene')) myzigate.store_scene(addr, endpoint, groupaddr, scene) def recall_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') scene = _to_int(service.data.get('scene')) myzigate.recall_scene(addr, endpoint, groupaddr, scene) def scene_membership_request(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') myzigate.scene_membership_request(addr, endpoint, groupaddr) def copy_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) fromgroupaddr = service.data.get('from_group_addr') fromscene = _to_int(service.data.get('from_scene')) togroupaddr = service.data.get('to_group_addr') toscene = _to_int(service.data.get('to_scene')) myzigate.copy_scene(addr, endpoint, fromgroupaddr, fromscene, togroupaddr, toscene) def ias_warning(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) mode = service.data.get('mode', 'burglar') strobe = service.data.get('strobe', True) level = service.data.get('level', 'low') duration = service.data.get('duration', 60) strobe_cycle = service.data.get('strobe_cycle', 10) strobe_level = service.data.get('strobe_level', 'low') myzigate.action_ias_warning(addr, endpoint, mode, strobe, level, duration, strobe_cycle, strobe_level) def ias_squawk(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) mode = service.data.get('mode', 'armed') strobe = service.data.get('strobe', True) level = service.data.get('level', 'low') myzigate.action_ias_squawk(addr, endpoint, mode, strobe, level) def upgrade_firmware(service): from zigate.flasher import flash from zigate.firmware import download_latest port = myzigate._port pizigate = False if isinstance(myzigate, zigate.ZiGateGPIO): pizigate = True if myzigate._started and not pizigate: msg = 'You should stop zigate first using service zigate.stop_zigate and put zigate in download mode.' hass.components.persistent_notification.create(msg, title='ZiGate') return if pizigate: stop_zigate() myzigate.set_bootloader_mode() backup_filename = 'zigate_backup_{:%Y%m%d%H%M%S}.bin'.format( datetime.datetime.now()) backup_filename = os.path.join(hass.config.config_dir, backup_filename) flash(port, save=backup_filename) msg = 'ZiGate backup created {}'.format(backup_filename) hass.components.persistent_notification.create(msg, title='ZiGate') firmware_path = service.data.get('path') if not firmware_path: firmware_path = download_latest() flash(port, write=firmware_path) msg = 'ZiGate flashed with {}'.format(firmware_path) hass.components.persistent_notification.create(msg, title='ZiGate') myzigate._version = None if pizigate: myzigate.set_running_mode() start_zigate() else: msg = 'Now you have to unplug/replug the ZiGate USB key and then call service zigate.start_zigate' hass.components.persistent_notification.create(msg, title='ZiGate') hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_zigate) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_zigate) hass.services.register(DOMAIN, 'refresh_devices_list', refresh_devices_list) hass.services.register(DOMAIN, 'generate_templates', generate_templates) hass.services.register(DOMAIN, 'reset', zigate_reset) hass.services.register(DOMAIN, 'permit_join', permit_join) hass.services.register(DOMAIN, 'start_zigate', start_zigate) hass.services.register(DOMAIN, 'stop_zigate', stop_zigate) hass.services.register(DOMAIN, 'cleanup_devices', zigate_cleanup) hass.services.register(DOMAIN, 'refresh_device', refresh_device, schema=REFRESH_DEVICE_SCHEMA) hass.services.register(DOMAIN, 'discover_device', discover_device, schema=DISCOVER_DEVICE_SCHEMA) hass.services.register(DOMAIN, 'network_scan', network_scan) hass.services.register(DOMAIN, 'raw_command', raw_command, schema=RAW_COMMAND_SCHEMA) hass.services.register(DOMAIN, 'identify_device', identify_device, schema=IDENTIFY_SCHEMA) hass.services.register(DOMAIN, 'remove_device', remove_device, schema=REMOVE_SCHEMA) hass.services.register(DOMAIN, 'initiate_touchlink', initiate_touchlink) hass.services.register(DOMAIN, 'touchlink_factory_reset', touchlink_factory_reset) hass.services.register(DOMAIN, 'read_attribute', read_attribute, schema=READ_ATTRIBUTE_SCHEMA) hass.services.register(DOMAIN, 'write_attribute', write_attribute, schema=WRITE_ATTRIBUTE_SCHEMA) hass.services.register(DOMAIN, 'add_group', add_group, schema=ADD_GROUP_SCHEMA) hass.services.register(DOMAIN, 'get_group_membership', get_group_membership, schema=GET_GROUP_MEMBERSHIP_SCHEMA) hass.services.register(DOMAIN, 'remove_group', remove_group, schema=REMOVE_GROUP_SCHEMA) hass.services.register(DOMAIN, 'action_onoff', action_onoff, schema=ACTION_ONOFF_SCHEMA) hass.services.register(DOMAIN, 'build_network_table', build_network_table, schema=BUILD_NETWORK_TABLE_SCHEMA) hass.services.register(DOMAIN, 'ias_warning', ias_warning, schema=ACTION_IAS_WARNING_SCHEMA) hass.services.register(DOMAIN, 'ias_squawk', ias_squawk, schema=ACTION_IAS_SQUAWK_SCHEMA) hass.services.register(DOMAIN, 'ota_load_image', ota_load_image, schema=OTA_LOAD_IMAGE_SCHEMA) hass.services.register(DOMAIN, 'ota_image_notify', ota_image_notify, schema=OTA_IMAGE_NOTIFY_SCHEMA) hass.services.register(DOMAIN, 'ota_get_status', get_ota_status) hass.services.register(DOMAIN, 'view_scene', view_scene, schema=VIEW_SCENE_SCHEMA) hass.services.register(DOMAIN, 'add_scene', add_scene, schema=ADD_SCENE_SCHEMA) hass.services.register(DOMAIN, 'remove_scene', remove_scene, schema=REMOVE_SCENE_SCHEMA) hass.services.register(DOMAIN, 'store_scene', store_scene, schema=STORE_SCENE_SCHEMA) hass.services.register(DOMAIN, 'recall_scene', recall_scene, schema=RECALL_SCENE_SCHEMA) hass.services.register(DOMAIN, 'scene_membership_request', scene_membership_request, schema=SCENE_MEMBERSHIP_REQUEST_SCHEMA) hass.services.register(DOMAIN, 'copy_scene', copy_scene, schema=COPY_SCENE_SCHEMA) hass.services.register(DOMAIN, 'upgrade_firmware', upgrade_firmware) track_time_change(hass, refresh_devices_list, hour=0, minute=0, second=0) if admin_panel: _LOGGER.debug('Start ZiGate Admin Panel on port 9998') myzigate.start_adminpanel(prefix='/zigateproxy') hass.http.register_view(ZiGateAdminPanel()) hass.http.register_view(ZiGateProxy()) custom_panel_config = { "name": "zigateadmin", "embed_iframe": False, "trust_external": False, "html_url": "/zigateadmin.html", } config = {} config["_panel_custom"] = custom_panel_config hass.components.frontend.async_register_built_in_panel( component_name="custom", sidebar_title='Zigate Admin', sidebar_icon='mdi:zigbee', frontend_url_path="zigateadmin", config=config, require_admin=True, ) # hass.async_create_task( # hass.config_entries.flow.async_init( # DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={} # ) # ) return True
import logging import zigate #https://pypi.org/project/zigate/ logging.basicConfig() logging.root.setLevel(logging.DEBUG) logging. z = zigate.connect(port=None) version = z.get_version_text() print(version) z.get_devices_list() devices = z.devices print(devices) #ZiGate has not been found...
def setup(hass, config): """Setup zigate platform.""" import zigate port = config[DOMAIN].get(CONF_PORT) host = config[DOMAIN].get(CONF_HOST) gpio = config[DOMAIN].get('gpio', False) enable_led = config[DOMAIN].get('enable_led', True) polling = config[DOMAIN].get('polling', True) channel = config[DOMAIN].get('channel') persistent_file = os.path.join(hass.config.config_dir, 'zigate.json') _LOGGER.debug('Port : %s', port) _LOGGER.debug('Host : %s', host) _LOGGER.debug('GPIO : %s', gpio) _LOGGER.debug('Led : %s', enable_led) _LOGGER.debug('Channel : %s', channel) myzigate = zigate.connect(port=port, host=host, path=persistent_file, auto_start=False, gpio=gpio ) _LOGGER.debug('ZiGate object created %s', myzigate) hass.data[DOMAIN] = myzigate hass.data[DATA_ZIGATE_DEVICES] = {} hass.data[DATA_ZIGATE_ATTRS] = {} component = EntityComponent(_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_ZIGATE) component.setup(config) entity = ZiGateComponentEntity(myzigate) hass.data[DATA_ZIGATE_DEVICES]['zigate'] = entity component.add_entities([entity]) def device_added(**kwargs): device = kwargs['device'] _LOGGER.debug('Add device {}'.format(device)) ieee = device.ieee or device.addr # compatibility if ieee not in hass.data[DATA_ZIGATE_DEVICES]: entity = ZiGateDeviceEntity(hass, device, polling) hass.data[DATA_ZIGATE_DEVICES][ieee] = entity component.add_entities([entity]) if 'signal' in kwargs: hass.components.persistent_notification.create( ('A new ZiGate device "{}"' ' has been added !' ).format(device), title='ZiGate') def device_removed(**kwargs): # component.async_remove_entity device = kwargs['device'] ieee = device.ieee or device.addr # compatibility hass.components.persistent_notification.create( 'The ZiGate device {}({}) is gone.'.format(device.ieee, device.addr), title='ZiGate') entity = hass.data[DATA_ZIGATE_DEVICES][ieee] component.async_remove_entity(entity.entity_id) del hass.data[DATA_ZIGATE_DEVICES][ieee] def device_need_discovery(**kwargs): device = kwargs['device'] hass.components.persistent_notification.create( ('The ZiGate device {}({}) needs to be discovered' ' (missing important' ' information)').format(device.ieee, device.addr), title='ZiGate') zigate.dispatcher.connect(device_added, zigate.ZIGATE_DEVICE_ADDED, weak=False) zigate.dispatcher.connect(device_removed, zigate.ZIGATE_DEVICE_REMOVED, weak=False) zigate.dispatcher.connect(device_need_discovery, zigate.ZIGATE_DEVICE_NEED_DISCOVERY, weak=False) def attribute_updated(**kwargs): device = kwargs['device'] ieee = device.ieee or device.addr # compatibility attribute = kwargs['attribute'] _LOGGER.debug('Update attribute for device {} {}'.format(device, attribute)) entity = hass.data[DATA_ZIGATE_DEVICES].get(ieee) event_data = attribute.copy() if type(event_data.get('type')) == type: event_data['type'] = event_data['type'].__name__ event_data['ieee'] = device.ieee event_data['addr'] = device.addr event_data['device_type'] = device.get_property_value('type') if entity: event_data['entity_id'] = entity.entity_id hass.bus.fire('zigate.attribute_updated', event_data) zigate.dispatcher.connect(attribute_updated, zigate.ZIGATE_ATTRIBUTE_UPDATED, weak=False) def device_updated(**kwargs): device = kwargs['device'] _LOGGER.debug('Update device {}'.format(device)) ieee = device.ieee or device.addr # compatibility entity = hass.data[DATA_ZIGATE_DEVICES].get(ieee) if not entity: _LOGGER.debug('Device not found {}, adding it'.format(device)) device_added(device=device) event_data = {} event_data['ieee'] = device.ieee event_data['addr'] = device.addr event_data['device_type'] = device.get_property_value('type') if entity: event_data['entity_id'] = entity.entity_id hass.bus.fire('zigate.device_updated', event_data) zigate.dispatcher.connect(device_updated, zigate.ZIGATE_DEVICE_UPDATED, weak=False) zigate.dispatcher.connect(device_updated, zigate.ZIGATE_ATTRIBUTE_ADDED, weak=False) zigate.dispatcher.connect(device_updated, zigate.ZIGATE_DEVICE_ADDRESS_CHANGED, weak=False) def zigate_reset(service): myzigate.reset() def permit_join(service): myzigate.permit_join() def zigate_cleanup(service): ''' Remove missing device ''' myzigate.cleanup_devices() def start_zigate(service_event=None): myzigate.autoStart(channel) myzigate.start_auto_save() myzigate.set_led(enable_led) version = myzigate.get_version_text() if version < '3.0f': hass.components.persistent_notification.create( ('Your zigate firmware is outdated, ' 'Please upgrade to 3.0f or later !'), title='ZiGate') # first load for device in myzigate.devices: device_added(device=device) for platform in SUPPORTED_PLATFORMS: load_platform(hass, platform, DOMAIN, {}, config) hass.bus.fire('zigate.started') def stop_zigate(service_event): myzigate.save_state() myzigate.close() hass.bus.fire('zigate.stopped') def refresh_devices_list(service): myzigate.get_devices_list() def generate_templates(service): myzigate.generate_templates(hass.config.config_dir) def _get_addr_from_service_request(service): entity_id = service.data.get(ATTR_ENTITY_ID) ieee = service.data.get(IEEE) addr = service.data.get(ADDR) if entity_id: entity = component.get_entity(entity_id) if entity: addr = entity._device.addr elif ieee: device = myzigate.get_device_from_ieee(ieee) if device: addr = device.addr return addr def _to_int(value): ''' convert str to int ''' if 'x' in value: return int(value, 16) return int(value) def refresh_device(service): addr = _get_addr_from_service_request(service) if addr: myzigate.refresh_device(addr, full=True) else: for device in myzigate.devices: device.refresh_device(full=True) def discover_device(service): addr = _get_addr_from_service_request(service) if addr: myzigate.discover_device(addr, True) def network_scan(service): myzigate.start_network_scan() def raw_command(service): cmd = _to_int(service.data.get('cmd')) data = service.data.get('data', '') myzigate.send_data(cmd, data) def identify_device(service): addr = _get_addr_from_service_request(service) myzigate.identify_device(addr) def remove_device(service): addr = _get_addr_from_service_request(service) myzigate.remove_device(addr) def initiate_touchlink(service): myzigate.initiate_touchlink() def touchlink_factory_reset(service): myzigate.touchlink_factory_reset() def read_attribute(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint')) cluster = _to_int(service.data.get('cluster')) attribute_id = _to_int(service.data.get('attribute_id')) manufacturer_code = _to_int(service.data.get('manufacturer_code', '0')) myzigate.read_attribute_request(addr, endpoint, cluster, attribute_id, manufacturer_code=manufacturer_code) def write_attribute(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint')) cluster = _to_int(service.data.get('cluster')) attribute_id = _to_int(service.data.get('attribute_id')) attribute_type = _to_int(service.data.get('attribute_type')) value = _to_int(service.data.get('value')) attributes = [(attribute_id, attribute_type, value)] manufacturer_code = _to_int(service.data.get('manufacturer_code', '0')) myzigate.write_attribute_request(addr, endpoint, cluster, attributes, manufacturer_code=manufacturer_code) def add_group(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint')) groupaddr = service.data.get('group_addr') myzigate.add_group(addr, endpoint, groupaddr) def remove_group(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint')) groupaddr = service.data.get('group_addr') myzigate.remove_group(addr, endpoint, groupaddr) def get_group_membership(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint')) myzigate.get_group_membership(addr, endpoint) def action_onoff(service): addr = _get_addr_from_service_request(service) onoff = _to_int(service.data.get('onoff')) endpoint = _to_int(service.data.get('endpoint', '0')) ontime = _to_int(service.data.get('on_time', '0')) offtime = _to_int(service.data.get('off_time', '0')) effect = _to_int(service.data.get('effect', '0')) gradient = _to_int(service.data.get('gradient', '0')) myzigate.action_onoff(addr, endpoint, onoff, ontime, offtime, effect, gradient) def build_network_table(service): table = myzigate.build_neighbours_table(service.data.get('force', False)) _LOGGER.debug('Neighbours table {}'.format(table)) entity = hass.data[DATA_ZIGATE_DEVICES].get('zigate') if entity: entity.network_table = table def ota_load_image(service): ota_image_path = service.data.get('imagepath') myzigate.ota_load_image(ota_image_path) def ota_image_notify(service): addr = _get_addr_from_service_request(service) destination_endpoint = _to_int(service.data.get('destination_endpoint', '1')) payload_type = _to_int(service.data.get('payload_type', '0')) myzigate.ota_image_notify(addr, destination_endpoint, payload_type) def get_ota_status(service): myzigate.get_ota_status() def view_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') scene = _to_int(service.data.get('scene')) myzigate.view_scene(addr, endpoint, groupaddr, scene) def add_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') scene = _to_int(service.data.get('scene')) name = service.data.get('scene_name') transition = _to_int(service.data.get('transition', '0')) myzigate.add_scene(addr, endpoint, groupaddr, scene, name, transition) def remove_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') scene = _to_int(service.data.get('scene', -1)) if scene == -1: scene = None myzigate.remove_scene(addr, endpoint, groupaddr, scene) def store_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') scene = _to_int(service.data.get('scene')) myzigate.store_scene(addr, endpoint, groupaddr, scene) def recall_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') scene = _to_int(service.data.get('scene')) myzigate.recall_scene(addr, endpoint, groupaddr, scene) def scene_membership_request(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) groupaddr = service.data.get('group_addr') myzigate.scene_membership_request(addr, endpoint, groupaddr) def copy_scene(service): addr = _get_addr_from_service_request(service) endpoint = _to_int(service.data.get('endpoint', '1')) fromgroupaddr = service.data.get('from_group_addr') fromscene = _to_int(service.data.get('from_scene')) togroupaddr = service.data.get('to_group_addr') toscene = _to_int(service.data.get('to_scene')) myzigate.copy_scene(addr, endpoint, fromgroupaddr, fromscene, togroupaddr, toscene) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_zigate) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_zigate) hass.services.register(DOMAIN, 'refresh_devices_list', refresh_devices_list) hass.services.register(DOMAIN, 'generate_templates', generate_templates) hass.services.register(DOMAIN, 'reset', zigate_reset) hass.services.register(DOMAIN, 'permit_join', permit_join) hass.services.register(DOMAIN, 'start_zigate', start_zigate) hass.services.register(DOMAIN, 'stop_zigate', stop_zigate) hass.services.register(DOMAIN, 'cleanup_devices', zigate_cleanup) hass.services.register(DOMAIN, 'refresh_device', refresh_device, schema=REFRESH_DEVICE_SCHEMA) hass.services.register(DOMAIN, 'discover_device', discover_device, schema=DISCOVER_DEVICE_SCHEMA) hass.services.register(DOMAIN, 'network_scan', network_scan) hass.services.register(DOMAIN, 'raw_command', raw_command, schema=RAW_COMMAND_SCHEMA) hass.services.register(DOMAIN, 'identify_device', identify_device, schema=IDENTIFY_SCHEMA) hass.services.register(DOMAIN, 'remove_device', remove_device, schema=REMOVE_SCHEMA) hass.services.register(DOMAIN, 'initiate_touchlink', initiate_touchlink) hass.services.register(DOMAIN, 'touchlink_factory_reset', touchlink_factory_reset) hass.services.register(DOMAIN, 'read_attribute', read_attribute, schema=READ_ATTRIBUTE_SCHEMA) hass.services.register(DOMAIN, 'write_attribute', write_attribute, schema=WRITE_ATTRIBUTE_SCHEMA) hass.services.register(DOMAIN, 'add_group', add_group, schema=ADD_GROUP_SCHEMA) hass.services.register(DOMAIN, 'get_group_membership', get_group_membership, schema=GET_GROUP_MEMBERSHIP_SCHEMA) hass.services.register(DOMAIN, 'remove_group', remove_group, schema=REMOVE_GROUP_SCHEMA) hass.services.register(DOMAIN, 'action_onoff', action_onoff, schema=ACTION_ONOFF_SCHEMA) hass.services.register(DOMAIN, 'build_network_table', build_network_table, schema=BUILD_NETWORK_TABLE_SCHEMA) hass.services.register(DOMAIN, 'ota_load_image', ota_load_image, schema=OTA_LOAD_IMAGE_SCHEMA) hass.services.register(DOMAIN, 'ota_image_notify', ota_image_notify, schema=OTA_IMAGE_NOTIFY_SCHEMA) hass.services.register(DOMAIN, 'ota_get_status', get_ota_status) hass.services.register(DOMAIN, 'view_scene', view_scene, schema=VIEW_SCENE_SCHEMA) hass.services.register(DOMAIN, 'add_scene', add_scene, schema=ADD_SCENE_SCHEMA) hass.services.register(DOMAIN, 'remove_scene', remove_scene, schema=REMOVE_SCENE_SCHEMA) hass.services.register(DOMAIN, 'store_scene', store_scene, schema=STORE_SCENE_SCHEMA) hass.services.register(DOMAIN, 'recall_scene', recall_scene, schema=RECALL_SCENE_SCHEMA) hass.services.register(DOMAIN, 'scene_membership_request', scene_membership_request, schema=SCENE_MEMBERSHIP_REQUEST_SCHEMA) hass.services.register(DOMAIN, 'copy_scene', copy_scene, schema=COPY_SCENE_SCHEMA) track_time_change(hass, refresh_devices_list, hour=0, minute=0, second=0) return True
device = d.to_json() device['friendly_name'] = str(d) devices.append(device) return {'devices': devices} @app.route('/api/network_table', name='api_network_table') def network_table(): force = bottle.request.query.get('force', 'false') == 'true' return {'network_table': zigate_instance.build_neighbours_table(force)} kwargs = {'host': '0.0.0.0', 'port': port, 'quiet': quiet, 'debug': debug} if autostart: r_app = app if mount: root_app = bottle.Bottle() root_app.mount(mount, app) r_app = root_app if daemon: t = threading.Thread(target=r_app.run, kwargs=kwargs, daemon=True) t.start() else: r_app.run(**kwargs) return app if __name__ == '__main__': import zigate zigate_instance = zigate.connect('fake') start_adminpanel(zigate_instance, daemon=False, quiet=False, debug=True)
# if you want logging import logging logging.basicConfig() logging.root.setLevel(logging.DEBUG) import zigate z = zigate.connect(port=None) # Leave None to auto-discover the port print(z.get_version()) OrderedDict([('major', 1), ('installer', '30c'), ('rssi', 0), ('version', '3.0c')]) print(z.get_version_text()) # refresh devices list z.get_devices_list() # start inclusion mode z.permit_join() z.is_permitting_join() True # list devices z.devices [Device '677c' , Device 'b8ce' , Device '92a7' , Device '59ef' ] z.devices[0].addr '677c' # get all discovered endpoints >>> z.devices[0].endpoints {1: {
def setup(hass, config): """Setup zigate platform.""" import zigate port = config[DOMAIN].get(CONF_PORT) host = config[DOMAIN].get(CONF_HOST) persistent_file = os.path.join(hass.config.config_dir, '.zigate.json') myzigate = zigate.connect(port=port, host=host, path=persistent_file, auto_start=False) # if host: # host = host.split(':', 1) # port = None # if len(host) == 2: # port = int(host[1]) # myzigate = zigate.ZiGateWiFi(host[0], # port, # path=persistent_file, # auto_start=False) # else: # myzigate = zigate.ZiGate(port, # path=persistent_file, # auto_start=False) hass.data[DOMAIN] = myzigate hass.data[DATA_ZIGATE_DEVICES] = {} hass.data[DATA_ZIGATE_ATTRS] = {} component = EntityComponent(_LOGGER, DOMAIN, hass) def device_added(**kwargs): device = kwargs['device'] _LOGGER.debug('Add device {}'.format(device)) if device.addr not in hass.data[DATA_ZIGATE_DEVICES]: entity = ZiGateDeviceEntity(device) hass.data[DATA_ZIGATE_DEVICES][device.addr] = entity component.add_entities([entity]) if 'signal' in kwargs: hass.components.persistent_notification.create( ('A new ZiGate device "{}"' ' has been added !').format(device), title='ZiGate') def device_removed(**kwargs): # component.async_remove_entity addr = kwargs['addr'] hass.components.persistent_notification.create( 'The ZiGate device {} has leaved.'.format(addr), title='ZiGate') def device_need_refresh(**kwargs): device = kwargs['device'] hass.components.persistent_notification.create( ('The ZiGate device {} needs some' ' refresh (missing important' ' information)').format(device.addr), title='ZiGate') zigate.dispatcher.connect(device_added, zigate.ZIGATE_DEVICE_ADDED, weak=False) zigate.dispatcher.connect(device_removed, zigate.ZIGATE_DEVICE_REMOVED, weak=False) zigate.dispatcher.connect(device_need_refresh, zigate.ZIGATE_DEVICE_NEED_REFRESH, weak=False) def attribute_updated(**kwargs): device = kwargs['device'] attribute = kwargs['attribute'] _LOGGER.debug('Update attribute for device {} {}'.format( device, attribute)) key = '{}-{}-{}-{}'.format( device.addr, attribute['endpoint'], attribute['cluster'], attribute['attribute'], ) entity = hass.data[DATA_ZIGATE_ATTRS].get(key) if entity: if entity.hass: entity.schedule_update_ha_state() key = '{}-{}-{}'.format( device.addr, 'switch', attribute['endpoint'], ) entity = hass.data[DATA_ZIGATE_ATTRS].get(key) if entity: if entity.hass: entity.schedule_update_ha_state() key = '{}-{}-{}'.format( device.addr, 'light', attribute['endpoint'], ) entity = hass.data[DATA_ZIGATE_ATTRS].get(key) if entity: if entity.hass: entity.schedule_update_ha_state() entity = hass.data[DATA_ZIGATE_DEVICES].get(device.addr) if entity: if entity.hass: entity.schedule_update_ha_state() zigate.dispatcher.connect(attribute_updated, zigate.ZIGATE_ATTRIBUTE_UPDATED, weak=False) def device_updated(**kwargs): device = kwargs['device'] _LOGGER.debug('Update device {}'.format(device)) entity = hass.data[DATA_ZIGATE_DEVICES].get(device.addr) if entity: if entity.hass: entity.schedule_update_ha_state() else: _LOGGER.debug('Device not found {}, adding it'.format(device)) device_added(device=device) zigate.dispatcher.connect(device_updated, zigate.ZIGATE_DEVICE_UPDATED, weak=False) zigate.dispatcher.connect(device_updated, zigate.ZIGATE_ATTRIBUTE_ADDED, weak=False) def zigate_reset(service): myzigate.reset() def permit_join(service): myzigate.permit_join() def zigate_cleanup(service): ''' Remove missing device ''' myzigate.cleanup_devices() def start_zigate(service_event=None): myzigate.autoStart() myzigate.start_auto_save() version = myzigate.get_version_text() if version < '3.0d': hass.components.persistent_notification.create( ('Your zigate firmware is outdated, ' 'Please upgrade to 3.0d or later !'), title='ZiGate') # first load for device in myzigate.devices: device_added(device=device) for platform in SUPPORTED_PLATFORMS: load_platform(hass, platform, DOMAIN, {}, config) def stop_zigate(service_event): myzigate.save_state() myzigate.close() def refresh_devices_list(service): myzigate.get_devices_list() def refresh_device(service): addr = service.data.get(ADDR) entity_id = service.data.get(ATTR_ENTITY_ID) if entity_id: entity = component.get_entity(entity_id) if entity: addr = entity._device.addr if addr: myzigate.refresh_device(addr) else: for device in myzigate.devices: device.refresh_device() def network_scan(service): myzigate.start_network_scan() def raw_command(service): cmd = int(service.data.get('cmd'), 16) data = service.data.get('data', '') myzigate.send_data(cmd, data) def identify_device(service): addr = service.data.get('addr') myzigate.identify_device(addr) def initiate_touchlink(service): myzigate.initiate_touchlink() def touchlink_factory_reset(service): myzigate.touchlink_factory_reset() hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_zigate) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_zigate) hass.services.register(DOMAIN, 'refresh_devices_list', refresh_devices_list) hass.services.register(DOMAIN, 'reset', zigate_reset) hass.services.register(DOMAIN, 'permit_join', permit_join) hass.services.register(DOMAIN, 'start_zigate', start_zigate) hass.services.register(DOMAIN, 'stop_zigate', stop_zigate) hass.services.register(DOMAIN, 'cleanup_devices', zigate_cleanup) hass.services.register(DOMAIN, 'refresh_device', refresh_device, schema=REFRESH_DEVICE_SCHEMA) hass.services.register(DOMAIN, 'network_scan', network_scan) hass.services.register(DOMAIN, 'raw_command', raw_command, schema=RAW_COMMAND_SCHEMA) hass.services.register(DOMAIN, 'identify_device', identify_device, schema=IDENTIFY_SCHEMA) hass.services.register(DOMAIN, 'initiate_touchlink', initiate_touchlink) hass.services.register(DOMAIN, 'touchlink_factory_reset', touchlink_factory_reset) track_time_change(hass, refresh_devices_list, hour=0, minute=0, second=0) return True
help='Enable Admin panel', default=True, action='store_true') parser.add_argument('--admin_panel_port', help='Admin panel url prefix', default=9998) parser.add_argument('--admin_panel_mount', help='Admin panel url mount point', default=None) parser.add_argument('--admin_panel_prefix', help='Admin panel url prefix', default=None) args = parser.parse_args() if args.debug: logging.root.setLevel(logging.DEBUG) z = connect(args.port, args.host, args.path, True, True, args.channel, args.gpio) if args.admin_panel: logging.root.info('Starting Admin Panel on port %s', args.admin_panel_port) if args.admin_panel_mount: logging.root.info('Mount point is %s', args.admin_panel_mount) if args.admin_panel_prefix: logging.root.info('URL prefix is %s', args.admin_panel_prefix) z.start_adminpanel(port=int(args.admin_panel_port), mount=args.admin_panel_mount, prefix=args.admin_panel_prefix, debug=args.debug) print('Press Ctrl+C to quit') try: while True: time.sleep(1) except KeyboardInterrupt: