Пример #1
0
def get_service(hass, config, discovery_info=None):
    """Get the HTML5 push notification service."""
    json_path = hass.config.path(REGISTRATIONS_FILE)

    registrations = _load_config(json_path)

    if registrations is None:
        return None

    vapid_pub_key = config.get(ATTR_VAPID_PUB_KEY)
    vapid_prv_key = config.get(ATTR_VAPID_PRV_KEY)
    vapid_email = config.get(ATTR_VAPID_EMAIL)

    def websocket_appkey(hass, connection, msg):
        connection.send_message(
            websocket_api.result_message(msg['id'], vapid_pub_key))

    hass.components.websocket_api.async_register_command(
        WS_TYPE_APPKEY, websocket_appkey, SCHEMA_WS_APPKEY
    )

    hass.http.register_view(
        HTML5PushRegistrationView(registrations, json_path))
    hass.http.register_view(HTML5PushCallbackView(registrations))

    gcm_api_key = config.get(ATTR_GCM_API_KEY)
    gcm_sender_id = config.get(ATTR_GCM_SENDER_ID)

    if gcm_sender_id is not None:
        add_manifest_json_key(
            ATTR_GCM_SENDER_ID, config.get(ATTR_GCM_SENDER_ID))

    return HTML5NotificationService(
        hass, gcm_api_key, vapid_prv_key, vapid_email, registrations,
        json_path)
Пример #2
0
def get_service(hass, config):
    """Get the HTML5 push notification service."""
    json_path = hass.config.path(REGISTRATIONS_FILE)

    registrations = _load_config(json_path)

    if registrations is None:
        return None

    hass.http.register_view(
        HTML5PushRegistrationView(hass, registrations, json_path))
    hass.http.register_view(HTML5PushCallbackView(hass, registrations))

    gcm_api_key = config.get(ATTR_GCM_API_KEY)
    gcm_sender_id = config.get(ATTR_GCM_SENDER_ID)

    if gcm_sender_id is not None:
        add_manifest_json_key(ATTR_GCM_SENDER_ID,
                              config.get(ATTR_GCM_SENDER_ID))

    return HTML5NotificationService(gcm_api_key, registrations)
Пример #3
0
def get_service(hass, config):
    """Get the HTML5 push notification service."""
    json_path = hass.config.path(REGISTRATIONS_FILE)

    registrations = _load_config(json_path)

    if registrations is None:
        return None

    hass.wsgi.register_view(
        HTML5PushRegistrationView(hass, registrations, json_path))
    hass.wsgi.register_view(HTML5PushCallbackView(hass, registrations))

    gcm_api_key = config.get(ATTR_GCM_API_KEY)
    gcm_sender_id = config.get(ATTR_GCM_SENDER_ID)

    if gcm_sender_id is not None:
        add_manifest_json_key(ATTR_GCM_SENDER_ID,
                              config.get(ATTR_GCM_SENDER_ID))

    return HTML5NotificationService(gcm_api_key, registrations)
Пример #4
0
def remove_hooks(hass):
    data = hass.data[DOMAIN]
    frontend.IndexView.get_template = data["get_template"]
    frontend.add_manifest_json_key("icons", data["manifest_icons"].copy())
    frontend.add_manifest_json_key("name", "Home Assistant")
    frontend.add_manifest_json_key("short_name", "Assistant")
    return True
Пример #5
0
async def apply_hooks(hass):
    data = hass.data.get(DOMAIN, {})
    icons = await hass.loop.run_in_executor(None, find_icons, hass,
                                            data.get(CONFIG_ICON_PATH, None))
    title = data.get(CONFIG_TITLE, None)

    def _get_template(self):
        tpl = data["get_template"](self)
        render = tpl.render

        def new_render(*args, **kwargs):
            text = render(*args, **kwargs)
            if "favicon" in icons:
                text = text.replace("/static/icons/favicon.ico",
                                    icons["favicon"])
            if "apple" in icons:
                text = text.replace("/static/icons/favicon-apple-180x180.png",
                                    icons["apple"])
            if title:
                text = text.replace("<title>Home Assistant</title>",
                                    f"<title>{title}</title>")
                text = text.replace("<body>", f"""
                    <body>
                        <script type="module">
                            customElements.whenDefined('ha-sidebar').then(() => {{
                                const Sidebar = customElements.get('ha-sidebar');
                                const updated = Sidebar.prototype.updated;
                                Sidebar.prototype.updated = function(changedProperties) {{
                                    updated.bind(this)(changedProperties);
                                    this.shadowRoot.querySelector(".title").innerHTML = "{title}";
                                }};
                            }});

                            window.setInterval(() => {{
                                if(!document.title.endsWith("- {title}") && document.title !== "{title}") {{
                                    document.title = document.title.replace(/Home Assistant/, "{title}");
                                }}
                            }}, 1000);
                        </script>
                    """

                                    # noqa: E501
                                    )

            return text

        tpl.render = new_render
        return tpl

    frontend.IndexView.get_template = _get_template
    for view in hass.http.app.router.resources():
        if isinstance(view, frontend.IndexView):
            view._template_cache = None

    if "manifest" in icons:
        frontend.add_manifest_json_key("icons", icons["manifest"])
    else:
        frontend.add_manifest_json_key("icons", data["manifest_icons"].copy())

    if title:
        frontend.add_manifest_json_key("name", title)
        frontend.add_manifest_json_key("short_name", title)
    else:
        frontend.add_manifest_json_key("name", "Home Assistant")
        frontend.add_manifest_json_key("short_name", "Assistant")

    return True