Expose create/delete cloudhook (#21606)

* Expose create/delete cloudhook

* Make sure we dont publish cloudhooks when not connected
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-03-03 19:03:49 -08:00
1 parent c25cbccca9
commit f5ed6432eb
6 files changed
+153 -6

No files matched your search

+47 -1
View File
@@ -6,17 +6,21 @@ import os
import voluptuous as vol
from homeassistant.core import callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, CLOUD_NEVER_EXPOSED_ENTITIES, CONF_REGION,
CONF_MODE, CONF_NAME)
from homeassistant.helpers import entityfilter, config_validation as cv
from homeassistant.loader import bind_hass
from homeassistant.util import dt as dt_util
from homeassistant.util.aiohttp import MockRequest
from homeassistant.components.alexa import smart_home as alexa_sh
from homeassistant.components.google_assistant import helpers as ga_h
from homeassistant.components.google_assistant import const as ga_c
from . import http_api, iot, auth_api, prefs, cloudhooks
from .const import CONFIG_DIR, DOMAIN, SERVERS
from .const import CONFIG_DIR, DOMAIN, SERVERS, STATE_CONNECTED
REQUIREMENTS = ['warrant==0.6.1']
@@ -81,6 +85,43 @@ CONFIG_SCHEMA = vol.Schema({
}, extra=vol.ALLOW_EXTRA)
class CloudNotAvailable(HomeAssistantError):
"""Raised when an action requires the cloud but it's not available."""
@bind_hass
@callback
def async_is_logged_in(hass):
"""Test if user is logged in."""
return DOMAIN in hass.data and hass.data[DOMAIN].is_logged_in
@bind_hass
async def async_create_cloudhook(hass, webhook_id):
"""Create a cloudhook."""
if not async_is_logged_in(hass):
raise CloudNotAvailable
return await hass.data[DOMAIN].cloudhooks.async_create(webhook_id)
@bind_hass
async def async_delete_cloudhook(hass, webhook_id):
"""Delete a cloudhook."""
if not async_is_logged_in(hass):
raise CloudNotAvailable
return await hass.data[DOMAIN].cloudhooks.async_delete(webhook_id)
def is_cloudhook_request(request):
"""Test if a request came from a cloudhook.
Async friendly.
"""
return isinstance(request, MockRequest)
async def async_setup(hass, config):
"""Initialize the Home Assistant cloud."""
if DOMAIN in config:
@@ -152,6 +193,11 @@ class Cloud:
"""Get if cloud is logged in."""
return self.id_token is not None
@property
def is_connected(self):
"""Get if cloud is connected."""
return self.iot.state == STATE_CONNECTED
@property
def subscription_expired(self):
"""Return a boolean if the subscription has expired."""
@@ -14,6 +14,9 @@ class Cloudhooks:
async def async_publish_cloudhooks(self):
"""Inform the Relayer of the cloudhooks that we support."""
if not self.cloud.is_connected:
return
cloudhooks = self.cloud.prefs.cloudhooks
await self.cloud.iot.async_send_message('webhook-register', {
'cloudhook_ids': [info['cloudhook_id'] for info
+4
View File
@@ -32,3 +32,7 @@ You have been logged out of Home Assistant Cloud because we have been unable
to verify your credentials. Please [log in](/config/cloud) again to continue
using the service.
"""
STATE_CONNECTING = 'connecting'
STATE_CONNECTED = 'connected'
STATE_DISCONNECTED = 'disconnected'
+4 -5
View File
@@ -16,15 +16,14 @@ from homeassistant.util.aiohttp import MockRequest
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from . import auth_api
from . import utils
from .const import MESSAGE_EXPIRATION, MESSAGE_AUTH_FAIL
from .const import (
MESSAGE_EXPIRATION, MESSAGE_AUTH_FAIL, STATE_CONNECTED, STATE_CONNECTING,
STATE_DISCONNECTED
)
HANDLERS = Registry()
_LOGGER = logging.getLogger(__name__)
STATE_CONNECTING = 'connecting'
STATE_CONNECTED = 'connected'
STATE_DISCONNECTED = 'disconnected'
class UnknownHandler(Exception):
"""Exception raised when trying to handle unknown handler."""