예제 #1
0
 def connect(self, device, baudrate=115200):
     assert self._zigate is None
     if '.' in device:  # supposed I.P:PORT
         host_port = device.split(':', 1)
         host = host_port[0]
         port = None
         if len(host_port) == 2:
             port = int(host_port[1])
         LOGGER.info('Configuring ZiGate WiFi {} {}'.format(host, port))
         self._zigate = zigate.ZiGateWiFi(host, port, auto_start=False)
     else:
         LOGGER.info('Configuring ZiGate USB {}'.format(device))
         self._zigate = zigate.ZiGate(device, auto_start=False)
if args.device == 'fake':
    z = zigate.core.FakeZiGate(args.device, persistent_file, auto_start=False)
elif '.' in args.device:  # supposed I.P:PORT
    host_port = args.device.split(':', 1)
    host = host_port[0]
    port = None
    if len(host_port) == 2:
        port = int(host_port[1])
    logging.info('Démarrage ZiGate WiFi {} {}'.format(host, port))
    z = zigate.ZiGateWiFi(host, port, persistent_file, auto_start=False)
else:
    logging.info('Démarrage ZiGate {}'.format(args.device))
    if args.gpio:
        z = zigate.ZiGateGPIO(args.device, persistent_file, auto_start=False)
    else:
        z = zigate.ZiGate(args.device, persistent_file, auto_start=False)
zigate.dispatcher.connect(callback_command, zigate.ZIGATE_DEVICE_ADDED, z)
zigate.dispatcher.connect(callback_command, zigate.ZIGATE_DEVICE_UPDATED, z)
zigate.dispatcher.connect(callback_command,
                          zigate.ZIGATE_DEVICE_ADDRESS_CHANGED, z)
zigate.dispatcher.connect(callback_command, zigate.ZIGATE_DEVICE_REMOVED, z)
zigate.dispatcher.connect(callback_command, zigate.ZIGATE_ATTRIBUTE_ADDED, z)
zigate.dispatcher.connect(callback_command, zigate.ZIGATE_ATTRIBUTE_UPDATED, z)
zigate.dispatcher.connect(callback_command,
                          zigate.ZIGATE_DEVICE_NEED_DISCOVERY, z)
zigate.dispatcher.connect(store_response,
                          zigate.ZIGATE_RESPONSE_RECEIVED,
                          weak=True)

z.autoStart(args.channel)
z.start_auto_save()
예제 #3
0
파일: white.py 프로젝트: ChoOo7/home
import logging
import collections
import time
from pydispatch import dispatcher
from collections import OrderedDict
logging.basicConfig()
logging.root.setLevel(logging.WARNING)

import zigate
z = zigate.ZiGate(port=None) # Leave None to auto-discover the port

z.actions_move_temperature('ee65', 3, 6500);
예제 #4
0
def setup(hass, config):
    """Setup zigate platform."""
    from homeassistant.components import persistent_notification
    import zigate

    port = config.get(CONF_PORT)
    host = config.get(CONF_HOST)
    persistent_file = os.path.join(hass.config.config_dir, 'zigate.json')
    _LOGGER.debug('Persistent file {}'.format(persistent_file))

    if host:
        if not port:
            port = 9999
        z = zigate.ZiGateWiFi(host,
                              port,
                              path=persistent_file,
                              auto_start=False)
    else:
        z = zigate.ZiGate(port, path=persistent_file, auto_start=False)

    hass.data[DOMAIN] = z
    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:
                persistent_notification.create(
                    hass, ('A new ZiGate device "{}"'
                           ' has been added !').format(device),
                    title='ZiGate')

    def device_removed(**kwargs):
        # component.async_remove_entity
        pass

    def device_need_refresh(**kwargs):
        device = kwargs['device']
        persistent_notification.create(hass,
                                       ('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):
        z.reset()

    def permit_join(service):
        z.permit_join()

    def zigate_cleanup(service):
        '''
        Remove missing device
        '''
        z.cleanup_devices()

    def start_zigate(service_event):
        z.autoStart()
        z.start_auto_save()
        # firt load
        for device in z.devices:
            device_added(device=device)

        load_platform(hass, 'sensor', DOMAIN, {}, config)
        load_platform(hass, 'binary_sensor', DOMAIN, {}, config)
        load_platform(hass, 'switch', DOMAIN, {}, config)
        load_platform(hass, 'light', DOMAIN, {}, config)

    def stop_zigate(service_event):
        z.save_state()
        z.close()

    def refresh_devices_list(service):
        z.get_devices_list()

    def refresh_device(service):
        addr = service.data.get(ADDR)
        if addr:
            z.refresh_device(addr)
        else:
            for device in z.devices:
                device.refresh_device()

    def network_scan(service):
        z.start_network_scan()

    def raw_command(service):
        cmd = int(service.data.get('cmd'))
        data = service.data.get('data', '')
        z.send_data(cmd, data)

    def identify_device(service):
        addr = service.data.get('addr')
        z.identify_device(addr)

    def initiate_touchlink(service):
        z.initiate_touchlink()

    def touchlink_factory_reset(service):
        z.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