Пример #1
0
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
    """Set up the Emby platform."""
    from pyemby import EmbyServer

    host = config.get(CONF_HOST)
    key = config.get(CONF_API_KEY)
    port = config.get(CONF_PORT)
    ssl = config.get(CONF_SSL)
    auto_hide = config.get(CONF_AUTO_HIDE)

    if port is None:
        port = DEFAULT_SSL_PORT if ssl else DEFAULT_PORT

    _LOGGER.debug("Setting up Emby server at: %s:%s", host, port)

    emby = EmbyServer(host, key, port, ssl, hass.loop)

    active_emby_devices = {}
    inactive_emby_devices = {}

    @callback
    def device_update_callback(data):
        """Handle devices which are added to Emby."""
        new_devices = []
        active_devices = []
        for dev_id in emby.devices:
            active_devices.append(dev_id)
            if (
                dev_id not in active_emby_devices
                and dev_id not in inactive_emby_devices
            ):
                new = EmbyDevice(emby, dev_id)
                active_emby_devices[dev_id] = new
                new_devices.append(new)

            elif dev_id in inactive_emby_devices:
                if emby.devices[dev_id].state != "Off":
                    add = inactive_emby_devices.pop(dev_id)
                    active_emby_devices[dev_id] = add
                    _LOGGER.debug("Showing %s, item: %s", dev_id, add)
                    add.set_available(True)
                    add.set_hidden(False)

        if new_devices:
            _LOGGER.debug("Adding new devices: %s", new_devices)
            async_add_entities(new_devices, True)

    @callback
    def device_removal_callback(data):
        """Handle the removal of devices from Emby."""
        if data in active_emby_devices:
            rem = active_emby_devices.pop(data)
            inactive_emby_devices[data] = rem
            _LOGGER.debug("Inactive %s, item: %s", data, rem)
            rem.set_available(False)
            if auto_hide:
                rem.set_hidden(True)

    @callback
    def start_emby(event):
        """Start Emby connection."""
        emby.start()

    async def stop_emby(event):
        """Stop Emby connection."""
        await emby.stop()

    emby.add_new_devices_callback(device_update_callback)
    emby.add_stale_devices_callback(device_removal_callback)

    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_emby)
    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_emby)
Пример #2
0
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
    """Set up the Emby platform."""
    from pyemby import EmbyServer

    host = config.get(CONF_HOST)
    key = config.get(CONF_API_KEY)
    port = config.get(CONF_PORT)
    ssl = config.get(CONF_SSL)
    auto_hide = config.get(CONF_AUTO_HIDE)

    if port is None:
        port = DEFAULT_SSL_PORT if ssl else DEFAULT_PORT

    _LOGGER.debug("Setting up Emby server at: %s:%s", host, port)

    emby = EmbyServer(host, key, port, ssl, hass.loop)

    active_emby_devices = {}
    inactive_emby_devices = {}

    @callback
    def device_update_callback(data):
        """Handle devices which are added to Emby."""
        new_devices = []
        active_devices = []
        for dev_id in emby.devices:
            active_devices.append(dev_id)
            if dev_id not in active_emby_devices and \
                    dev_id not in inactive_emby_devices:
                new = EmbyDevice(emby, dev_id)
                active_emby_devices[dev_id] = new
                new_devices.append(new)

            elif dev_id in inactive_emby_devices:
                if emby.devices[dev_id].state != 'Off':
                    add = inactive_emby_devices.pop(dev_id)
                    active_emby_devices[dev_id] = add
                    _LOGGER.debug("Showing %s, item: %s", dev_id, add)
                    add.set_available(True)
                    add.set_hidden(False)

        if new_devices:
            _LOGGER.debug("Adding new devices: %s", new_devices)
            async_add_devices(new_devices, update_before_add=True)

    @callback
    def device_removal_callback(data):
        """Handle the removal of devices from Emby."""
        if data in active_emby_devices:
            rem = active_emby_devices.pop(data)
            inactive_emby_devices[data] = rem
            _LOGGER.debug("Inactive %s, item: %s", data, rem)
            rem.set_available(False)
            if auto_hide:
                rem.set_hidden(True)

    @callback
    def start_emby(event):
        """Start Emby connection."""
        emby.start()

    @asyncio.coroutine
    def stop_emby(event):
        """Stop Emby connection."""
        yield from emby.stop()

    emby.add_new_devices_callback(device_update_callback)
    emby.add_stale_devices_callback(device_removal_callback)

    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_emby)
    hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_emby)