コード例 #1
0
ファイル: tellstick.py プロジェクト: zircop/home-assistant
def setup(hass, config):
    """Setup the Tellstick component."""
    from tellcore.constants import TELLSTICK_DIM
    from tellcore.telldus import AsyncioCallbackDispatcher
    from tellcore.telldus import TelldusCore

    try:
        tellcore_lib = TelldusCore(
            callback_dispatcher=AsyncioCallbackDispatcher(hass.loop))
    except OSError:
        _LOGGER.exception('Could not initialize Tellstick')
        return False

    # Get all devices, switches and lights alike
    all_tellcore_devices = tellcore_lib.devices()

    # Register devices
    tellcore_registry = TellstickRegistry(hass, tellcore_lib)
    tellcore_registry.register_tellcore_devices(all_tellcore_devices)
    hass.data['tellcore_registry'] = tellcore_registry

    # Discover the switches
    _discover(hass, config, 'switch', [
        tellcore_device.id for tellcore_device in all_tellcore_devices
        if not tellcore_device.methods(TELLSTICK_DIM)
    ])

    # Discover the lights
    _discover(hass, config, 'light', [
        tellcore_device.id for tellcore_device in all_tellcore_devices
        if tellcore_device.methods(TELLSTICK_DIM)
    ])

    return True
コード例 #2
0
    def setup(self):
        # Callback dispatcher
        # --------------------
        # Dispatcher for use with the event loop available in Python 3.4+.
        # Callbacks will be dispatched on the thread running the event loop.
        # The loop argument should be a BaseEventLoop instance, e.g. the one
        # returned from asyncio.get_event_loop().
        dispatcher = AsyncioCallbackDispatcher(self.context.loop)

        # Telldus core
        # --------------------
        # The main class for tellcore-py. Has methods for adding devices and for
        # enumerating controllers, devices and sensors. Also handles callbacks;
        # both registration and making sure the callbacks are processed in the
        # main thread instead of the callback thread.
        self.telldus = TelldusCore(callback_dispatcher=dispatcher)

        # Devices
        # --------------------
        # List of configured devices in /etc/tellstick.conf
        self.devices = self.get_devices()
        # self.last_seen = {}
        self.state.devices = {did: d.name for did, d in self.devices.items()}

        # Register event callback handlers
        self.telldus.register_device_event(self.callbacks.device_event)
        self.telldus.register_device_change_event(
            self.callbacks.device_change_event)
        self.telldus.register_raw_device_event(self.callbacks.raw_event)
コード例 #3
0
def setup(hass, config):
    """Set up the Tellstick component."""
    from tellcore.constants import TELLSTICK_DIM
    from tellcore.telldus import AsyncioCallbackDispatcher
    from tellcore.telldus import TelldusCore
    from tellcorenet import TellCoreClient

    conf = config.get(DOMAIN, {})
    net_host = conf.get(CONF_HOST)
    net_port = conf.get(CONF_PORT)

    # Initialize remote tellcore client
    if net_host and net_port:
        net_client = TellCoreClient(net_host, net_port)
        net_client.start()

        def stop_tellcore_net(event):
            """Event handler to stop the client."""
            net_client.stop()

        hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_tellcore_net)

    try:
        tellcore_lib = TelldusCore(
            callback_dispatcher=AsyncioCallbackDispatcher(hass.loop))
    except OSError:
        _LOGGER.exception("Could not initialize Tellstick")
        return False

    # Get all devices, switches and lights alike
    tellcore_devices = tellcore_lib.devices()

    # Register devices
    hass.data[DATA_TELLSTICK] = {device.id: device for
                                 device in tellcore_devices}

    # Discover the switches
    _discover(hass, config, 'switch',
              [device.id for device in tellcore_devices
               if not device.methods(TELLSTICK_DIM)])

    # Discover the lights
    _discover(hass, config, 'light',
              [device.id for device in tellcore_devices
               if device.methods(TELLSTICK_DIM)])

    @callback
    def async_handle_callback(tellcore_id, tellcore_command,
                              tellcore_data, cid):
        """Handle the actual callback from Tellcore."""
        hass.helpers.dispatcher.async_dispatcher_send(
            SIGNAL_TELLCORE_CALLBACK, tellcore_id,
            tellcore_command, tellcore_data)

    # Register callback
    callback_id = tellcore_lib.register_device_event(
        async_handle_callback)

    def clean_up_callback(event):
        """Unregister the callback bindings."""
        if callback_id is not None:
            tellcore_lib.unregister_callback(callback_id)

    hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, clean_up_callback)

    return True
コード例 #4
0
def setup(opp, config):
    """Set up the Tellstick component."""

    conf = config.get(DOMAIN, {})
    net_host = conf.get(CONF_HOST)
    net_ports = conf.get(CONF_PORT)

    # Initialize remote tellcore client
    if net_host:
        net_client = TellCoreClient(host=net_host,
                                    port_client=net_ports[0],
                                    port_events=net_ports[1])
        net_client.start()

        def stop_tellcore_net(event):
            """Event handler to stop the client."""
            net_client.stop()

        opp.bus.listen_once(EVENT_OPENPEERPOWER_STOP, stop_tellcore_net)

    try:
        tellcore_lib = TelldusCore(
            callback_dispatcher=AsyncioCallbackDispatcher(opp.loop))
    except OSError:
        _LOGGER.exception("Could not initialize Tellstick")
        return False

    # Get all devices, switches and lights alike
    tellcore_devices = tellcore_lib.devices()

    # Register devices
    opp.data[DATA_TELLSTICK] = {
        device.id: device
        for device in tellcore_devices
    }

    # Discover the lights
    _discover(
        opp,
        config,
        "light",
        [
            device.id
            for device in tellcore_devices if device.methods(TELLSTICK_DIM)
        ],
    )

    # Discover the cover
    _discover(
        opp,
        config,
        "cover",
        [
            device.id
            for device in tellcore_devices if device.methods(TELLSTICK_UP)
        ],
    )

    # Discover the switches
    _discover(
        opp,
        config,
        "switch",
        [
            device.id for device in tellcore_devices
            if (not device.methods(TELLSTICK_UP)
                and not device.methods(TELLSTICK_DIM))
        ],
    )

    @callback
    def async_handle_callback(tellcore_id, tellcore_command, tellcore_data,
                              cid):
        """Handle the actual callback from Tellcore."""
        opp.helpers.dispatcher.async_dispatcher_send(SIGNAL_TELLCORE_CALLBACK,
                                                     tellcore_id,
                                                     tellcore_command,
                                                     tellcore_data)

    # Register callback
    callback_id = tellcore_lib.register_device_event(async_handle_callback)

    def clean_up_callback(event):
        """Unregister the callback bindings."""
        if callback_id is not None:
            tellcore_lib.unregister_callback(callback_id)

    opp.bus.listen_once(EVENT_OPENPEERPOWER_STOP, clean_up_callback)

    return True