2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Nest devices."""
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2016-04-12 04:52:19 +00:00
|
|
|
import logging
|
|
|
|
|
2020-12-27 08:49:22 +00:00
|
|
|
from google_nest_sdm.event import EventMessage
|
2020-12-28 04:30:51 +00:00
|
|
|
from google_nest_sdm.exceptions import (
|
|
|
|
AuthException,
|
|
|
|
ConfigurationException,
|
|
|
|
GoogleNestException,
|
|
|
|
)
|
2020-10-21 08:17:49 +00:00
|
|
|
from google_nest_sdm.google_nest_subscriber import GoogleNestSubscriber
|
2016-04-01 14:31:11 +00:00
|
|
|
import voluptuous as vol
|
2016-09-01 20:08:03 +00:00
|
|
|
|
2021-04-10 05:41:29 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2017-04-30 05:04:49 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_BINARY_SENSORS,
|
2020-05-30 15:27:20 +00:00
|
|
|
CONF_CLIENT_ID,
|
|
|
|
CONF_CLIENT_SECRET,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_MONITORED_CONDITIONS,
|
|
|
|
CONF_SENSORS,
|
|
|
|
CONF_STRUCTURE,
|
|
|
|
)
|
2020-12-22 20:42:37 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-10 05:41:29 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2020-10-21 08:17:49 +00:00
|
|
|
from homeassistant.helpers import (
|
|
|
|
aiohttp_client,
|
|
|
|
config_entry_oauth2_flow,
|
|
|
|
config_validation as cv,
|
|
|
|
)
|
2016-01-14 04:05:47 +00:00
|
|
|
|
2020-12-22 20:42:37 +00:00
|
|
|
from . import api, config_flow
|
2021-01-02 00:51:01 +00:00
|
|
|
from .const import DATA_SDM, DATA_SUBSCRIBER, DOMAIN, OAUTH2_AUTHORIZE, OAUTH2_TOKEN
|
2020-11-30 08:19:42 +00:00
|
|
|
from .events import EVENT_NAME_MAP, NEST_EVENT
|
2020-12-22 20:42:37 +00:00
|
|
|
from .legacy import async_setup_legacy, async_setup_legacy_entry
|
2018-06-13 15:14:52 +00:00
|
|
|
|
2016-11-28 00:18:47 +00:00
|
|
|
_CONFIGURING = {}
|
2016-09-01 20:08:03 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-01-14 04:05:47 +00:00
|
|
|
|
2020-10-21 08:17:49 +00:00
|
|
|
CONF_PROJECT_ID = "project_id"
|
|
|
|
CONF_SUBSCRIBER_ID = "subscriber_id"
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_NEST_CONFIG = "nest_config"
|
2020-12-28 04:30:51 +00:00
|
|
|
DATA_NEST_UNAVAILABLE = "nest_unavailable"
|
|
|
|
|
|
|
|
NEST_SETUP_NOTIFICATION = "nest_setup"
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
SENSOR_SCHEMA = vol.Schema(
|
|
|
|
{vol.Optional(CONF_MONITORED_CONDITIONS): vol.All(cv.ensure_list)}
|
|
|
|
)
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_CLIENT_ID): cv.string,
|
|
|
|
vol.Required(CONF_CLIENT_SECRET): cv.string,
|
2020-10-21 08:17:49 +00:00
|
|
|
# Required to use the new API (optional for compatibility)
|
|
|
|
vol.Optional(CONF_PROJECT_ID): cv.string,
|
|
|
|
vol.Optional(CONF_SUBSCRIBER_ID): cv.string,
|
|
|
|
# Config that only currently works on the old API
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_STRUCTURE): vol.All(cv.ensure_list, [cv.string]),
|
|
|
|
vol.Optional(CONF_SENSORS): SENSOR_SCHEMA,
|
|
|
|
vol.Optional(CONF_BINARY_SENSORS): SENSOR_SCHEMA,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
2020-11-06 08:57:02 +00:00
|
|
|
# Platforms for SDM API
|
|
|
|
PLATFORMS = ["sensor", "camera", "climate"]
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2016-04-12 04:52:19 +00:00
|
|
|
|
2020-10-21 08:17:49 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
|
|
"""Set up Nest components with dispatch between old/new flows."""
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
|
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if CONF_PROJECT_ID not in config[DOMAIN]:
|
|
|
|
return await async_setup_legacy(hass, config)
|
|
|
|
|
|
|
|
if CONF_SUBSCRIBER_ID not in config[DOMAIN]:
|
|
|
|
_LOGGER.error("Configuration option '{CONF_SUBSCRIBER_ID}' required")
|
|
|
|
return False
|
|
|
|
|
|
|
|
# For setup of ConfigEntry below
|
|
|
|
hass.data[DOMAIN][DATA_NEST_CONFIG] = config[DOMAIN]
|
|
|
|
project_id = config[DOMAIN][CONF_PROJECT_ID]
|
|
|
|
config_flow.NestFlowHandler.register_sdm_api(hass)
|
|
|
|
config_flow.NestFlowHandler.async_register_implementation(
|
|
|
|
hass,
|
|
|
|
config_entry_oauth2_flow.LocalOAuth2Implementation(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
config[DOMAIN][CONF_CLIENT_ID],
|
|
|
|
config[DOMAIN][CONF_CLIENT_SECRET],
|
|
|
|
OAUTH2_AUTHORIZE.format(project_id=project_id),
|
|
|
|
OAUTH2_TOKEN,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-12-27 08:49:22 +00:00
|
|
|
class SignalUpdateCallback:
|
2020-10-21 08:17:49 +00:00
|
|
|
"""An EventCallback invoked when new events arrive from subscriber."""
|
|
|
|
|
|
|
|
def __init__(self, hass: HomeAssistant):
|
|
|
|
"""Initialize EventCallback."""
|
|
|
|
self._hass = hass
|
|
|
|
|
2020-11-24 15:53:50 +00:00
|
|
|
async def async_handle_event(self, event_message: EventMessage):
|
2020-10-21 08:17:49 +00:00
|
|
|
"""Process an incoming EventMessage."""
|
2020-11-24 22:34:43 +00:00
|
|
|
if not event_message.resource_update_name:
|
|
|
|
return
|
|
|
|
device_id = event_message.resource_update_name
|
2020-10-21 08:17:49 +00:00
|
|
|
events = event_message.resource_update_events
|
2020-11-24 22:34:43 +00:00
|
|
|
if not events:
|
|
|
|
return
|
|
|
|
_LOGGER.debug("Event Update %s", events.keys())
|
|
|
|
device_registry = await self._hass.helpers.device_registry.async_get_registry()
|
2021-01-07 12:49:45 +00:00
|
|
|
device_entry = device_registry.async_get_device({(DOMAIN, device_id)})
|
2020-11-24 22:34:43 +00:00
|
|
|
if not device_entry:
|
2020-10-21 08:17:49 +00:00
|
|
|
return
|
2020-11-24 22:34:43 +00:00
|
|
|
for event in events:
|
2020-11-30 08:19:42 +00:00
|
|
|
event_type = EVENT_NAME_MAP.get(event)
|
|
|
|
if not event_type:
|
2020-11-24 22:34:43 +00:00
|
|
|
continue
|
|
|
|
message = {
|
|
|
|
"device_id": device_entry.id,
|
2020-11-30 08:19:42 +00:00
|
|
|
"type": event_type,
|
2020-12-30 09:23:48 +00:00
|
|
|
"timestamp": event_message.timestamp,
|
2020-11-24 22:34:43 +00:00
|
|
|
}
|
|
|
|
self._hass.bus.async_fire(NEST_EVENT, message)
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Set up Nest from a config entry with dispatch between old/new flows."""
|
|
|
|
|
|
|
|
if DATA_SDM not in entry.data:
|
|
|
|
return await async_setup_legacy_entry(hass, entry)
|
|
|
|
|
|
|
|
implementation = (
|
|
|
|
await config_entry_oauth2_flow.async_get_config_entry_implementation(
|
|
|
|
hass, entry
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
config = hass.data[DOMAIN][DATA_NEST_CONFIG]
|
|
|
|
|
|
|
|
session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
|
|
|
|
auth = api.AsyncConfigEntryAuth(
|
|
|
|
aiohttp_client.async_get_clientsession(hass),
|
|
|
|
session,
|
2021-01-02 00:51:01 +00:00
|
|
|
config[CONF_CLIENT_ID],
|
|
|
|
config[CONF_CLIENT_SECRET],
|
2020-10-21 08:17:49 +00:00
|
|
|
)
|
|
|
|
subscriber = GoogleNestSubscriber(
|
|
|
|
auth, config[CONF_PROJECT_ID], config[CONF_SUBSCRIBER_ID]
|
|
|
|
)
|
2020-12-27 08:49:22 +00:00
|
|
|
callback = SignalUpdateCallback(hass)
|
|
|
|
subscriber.set_update_callback(callback.async_handle_event)
|
2020-11-19 11:26:49 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
await subscriber.start_async()
|
2020-12-20 00:41:29 +00:00
|
|
|
except AuthException as err:
|
|
|
|
_LOGGER.debug("Subscriber authentication error: %s", err)
|
2021-04-10 05:41:29 +00:00
|
|
|
raise ConfigEntryAuthFailed from err
|
2020-12-28 04:30:51 +00:00
|
|
|
except ConfigurationException as err:
|
|
|
|
_LOGGER.error("Configuration error: %s", err)
|
|
|
|
subscriber.stop_async()
|
|
|
|
return False
|
2020-11-19 11:26:49 +00:00
|
|
|
except GoogleNestException as err:
|
2020-12-28 04:30:51 +00:00
|
|
|
if DATA_NEST_UNAVAILABLE not in hass.data[DOMAIN]:
|
|
|
|
_LOGGER.error("Subscriber error: %s", err)
|
|
|
|
hass.data[DOMAIN][DATA_NEST_UNAVAILABLE] = True
|
2020-11-19 11:26:49 +00:00
|
|
|
subscriber.stop_async()
|
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
|
|
|
|
try:
|
|
|
|
await subscriber.async_get_device_manager()
|
|
|
|
except GoogleNestException as err:
|
2020-12-28 04:30:51 +00:00
|
|
|
if DATA_NEST_UNAVAILABLE not in hass.data[DOMAIN]:
|
|
|
|
_LOGGER.error("Device manager error: %s", err)
|
|
|
|
hass.data[DOMAIN][DATA_NEST_UNAVAILABLE] = True
|
2020-11-19 11:26:49 +00:00
|
|
|
subscriber.stop_async()
|
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
|
2020-12-28 04:30:51 +00:00
|
|
|
hass.data[DOMAIN].pop(DATA_NEST_UNAVAILABLE, None)
|
2020-11-30 08:19:42 +00:00
|
|
|
hass.data[DOMAIN][DATA_SUBSCRIBER] = subscriber
|
2020-10-21 08:17:49 +00:00
|
|
|
|
2021-04-27 18:42:21 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
if DATA_SDM not in entry.data:
|
|
|
|
# Legacy API
|
|
|
|
return True
|
2021-02-14 12:36:05 +00:00
|
|
|
_LOGGER.debug("Stopping nest subscriber")
|
2020-11-30 08:19:42 +00:00
|
|
|
subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER]
|
2020-10-21 08:17:49 +00:00
|
|
|
subscriber.stop_async()
|
2021-04-27 18:42:21 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-10-21 08:17:49 +00:00
|
|
|
if unload_ok:
|
2020-11-30 08:19:42 +00:00
|
|
|
hass.data[DOMAIN].pop(DATA_SUBSCRIBER)
|
2020-12-28 04:30:51 +00:00
|
|
|
hass.data[DOMAIN].pop(DATA_NEST_UNAVAILABLE, None)
|
2020-10-21 08:17:49 +00:00
|
|
|
|
|
|
|
return unload_ok
|