コード例 #1
0
def async_setup(hass, config):
    """Set up customizer."""
    maybe_load_panel(hass, config[DOMAIN].get(CONF_PANEL))

    custom_ui = config[DOMAIN].get(CONF_CUSTOM_UI)
    if MINOR_VERSION < 53 and custom_ui is not None:
        _LOGGER.warning('%s is only supported from Home Assistant 0.53',
                        CONF_CUSTOM_UI)
    elif custom_ui is not None:
        if custom_ui == LOCAL:
            frontend.add_extra_html_url(
                hass, '/local/custom_ui/state-card-custom-ui.html')
        elif custom_ui == HOSTED:
            frontend.add_extra_html_url(
                hass, 'https://raw.githubusercontent.com/andrey-git/'
                'home-assistant-custom-ui/master/state-card-custom-ui.html')
        else:
            frontend.add_extra_html_url(
                hass, 'https://github.com/andrey-git/home-assistant-custom-ui/'
                'releases/download/{}/state-card-custom-ui.html'.format(
                    custom_ui))

    component = EntityComponent(_LOGGER, DOMAIN, hass)
    if not config[DOMAIN][CONF_HIDE_CUSTOMUI_ATTRIBUTES]:
        if MINOR_VERSION >= 53:
            _LOGGER.error(
                '%s is deprecated. '
                'Starting from HA 0.53 it is always treated as True',
                CONF_HIDE_CUSTOMUI_ATTRIBUTES)
    yield from component.async_add_entity(CustomizerEntity(config[DOMAIN]))

    descriptions = yield from hass.async_add_job(
        load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    @callback
    def set_attribute(call):
        """Set attribute override."""
        data = call.data
        entity_id = data[CONF_ENTITY_ID]
        attribute = data[CONF_ATTRIBUTE]
        value = data.get(CONF_VALUE)
        overrides = hass.data[DATA_CUSTOMIZE].get(entity_id)
        state = hass.states.get(entity_id)
        state_attributes = dict(state.attributes)
        if value is None:
            if attribute in overrides:
                overrides.pop(attribute)
            if attribute in state_attributes:
                state_attributes.pop(attribute)
        else:
            overrides[attribute] = value
            state_attributes[attribute] = value
        hass.states.async_set(entity_id, state.state, state_attributes)

    hass.services.async_register(DOMAIN, SERVICE_SET_ATTRIBUTE, set_attribute,
                                 descriptions[SERVICE_SET_ATTRIBUTE],
                                 SERVICE_SET_ATTRIBUTE_SCHEMA)

    return True
コード例 #2
0
    def async_setup_platform(p_type, p_config=None, discovery_info=None):
        """Set up a mailbox platform."""
        if p_config is None:
            p_config = {}
        if discovery_info is None:
            discovery_info = {}

        platform = yield from async_prepare_setup_platform(
            hass, config, DOMAIN, p_type)

        if platform is None:
            _LOGGER.error("Unknown mailbox platform specified")
            return

        _LOGGER.info("Setting up %s.%s", DOMAIN, p_type)
        mailbox = None
        try:
            if hasattr(platform, 'async_get_handler'):
                mailbox = yield from \
                    platform.async_get_handler(hass, p_config, discovery_info)
            elif hasattr(platform, 'get_handler'):
                mailbox = yield from hass.async_add_job(
                    platform.get_handler, hass, p_config, discovery_info)
            else:
                raise HomeAssistantError("Invalid mailbox platform.")

            if mailbox is None:
                _LOGGER.error("Failed to initialize mailbox platform %s",
                              p_type)
                return

        except Exception:  # pylint: disable=broad-except
            _LOGGER.exception('Error setting up platform %s', p_type)
            return

        mailboxes.append(mailbox)
        mailbox_entity = MailboxEntity(hass, mailbox)
        component = EntityComponent(logging.getLogger(__name__), DOMAIN, hass,
                                    SCAN_INTERVAL)
        yield from component.async_add_entity(mailbox_entity)
コード例 #3
0
ファイル: __init__.py プロジェクト: Landrash/home-assistant
    def async_setup_platform(p_type, p_config=None, discovery_info=None):
        """Set up a mailbox platform."""
        if p_config is None:
            p_config = {}
        if discovery_info is None:
            discovery_info = {}

        platform = yield from async_prepare_setup_platform(
            hass, config, DOMAIN, p_type)

        if platform is None:
            _LOGGER.error("Unknown mailbox platform specified")
            return

        _LOGGER.info("Setting up %s.%s", DOMAIN, p_type)
        mailbox = None
        try:
            if hasattr(platform, 'async_get_handler'):
                mailbox = yield from \
                    platform.async_get_handler(hass, p_config, discovery_info)
            elif hasattr(platform, 'get_handler'):
                mailbox = yield from hass.async_add_job(
                    platform.get_handler, hass, p_config, discovery_info)
            else:
                raise HomeAssistantError("Invalid mailbox platform.")

            if mailbox is None:
                _LOGGER.error(
                    "Failed to initialize mailbox platform %s", p_type)
                return

        except Exception:  # pylint: disable=broad-except
            _LOGGER.exception('Error setting up platform %s', p_type)
            return

        mailboxes.append(mailbox)
        mailbox_entity = MailboxEntity(hass, mailbox)
        component = EntityComponent(
            logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL)
        yield from component.async_add_entity(mailbox_entity)
コード例 #4
0
def async_setup(hass, config):
    """Set up customizer."""
    custom_ui = config[DOMAIN].get(CONF_CUSTOM_UI)
    if MINOR_VERSION < 53 and custom_ui is not None:
        _LOGGER.warning('%s is only supported from Home Assistant 0.53',
                        CONF_CUSTOM_UI)
    elif custom_ui is not None:

        def add_extra_html_url(base_url):
            """Add extra url using version-dependent function."""
            if MINOR_VERSION >= 59:
                frontend.add_extra_html_url(hass, '{}.html'.format(base_url),
                                            False)
                frontend.add_extra_html_url(hass,
                                            '{}-es5.html'.format(base_url),
                                            True)
            else:
                frontend.add_extra_html_url(hass, '{}.html'.format(base_url))

        if custom_ui == LOCAL:
            add_extra_html_url('/local/custom_ui/state-card-custom-ui')
        elif custom_ui == HOSTED:
            add_extra_html_url(
                'https://raw.githubusercontent.com/andrey-git/'
                'home-assistant-custom-ui/master/state-card-custom-ui')
        elif custom_ui == DEBUG:
            add_extra_html_url('https://raw.githubusercontent.com/andrey-git/'
                               'home-assistant-custom-ui/master/'
                               'state-card-custom-ui-dbg')
        else:
            add_extra_html_url(
                'https://github.com/andrey-git/home-assistant-custom-ui/'
                'releases/download/{}/state-card-custom-ui'.format(custom_ui))

    component = EntityComponent(_LOGGER, DOMAIN, hass)
    yield from component.async_add_entity(CustomizerEntity(config[DOMAIN]))

    descriptions = yield from hass.async_add_job(
        load_yaml_config_file,
        os.path.join(os.path.dirname(__file__), 'services.yaml'))

    @callback
    def set_attribute(call):
        """Set attribute override."""
        data = call.data
        entity_id = data[CONF_ENTITY_ID]
        attribute = data[CONF_ATTRIBUTE]
        value = data.get(CONF_VALUE)
        overrides = hass.data[DATA_CUSTOMIZE].get(entity_id)
        state = hass.states.get(entity_id)
        state_attributes = dict(state.attributes)
        if value is None:
            if attribute in overrides:
                overrides.pop(attribute)
            if attribute in state_attributes:
                state_attributes.pop(attribute)
        else:
            overrides[attribute] = value
            state_attributes[attribute] = value
        hass.states.async_set(entity_id, state.state, state_attributes)

    hass.services.async_register(DOMAIN, SERVICE_SET_ATTRIBUTE, set_attribute,
                                 descriptions[SERVICE_SET_ATTRIBUTE],
                                 SERVICE_SET_ATTRIBUTE_SCHEMA)

    return True