From 0991a3012596543c996ff77ec84e3f44712d1cd3 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Sun, 14 Nov 2021 16:08:22 -0800 Subject: [PATCH] Pre-factor nest subscriber to library (#59462) * Pre-factor nest subscriber to library Move the nest subscriber to a library that can be reused in a future PR: - From ConfigFlow for creating subscriptions - On nest removal to delete subscriptions This is pulled out of PR #59260 to make that easier to review. * Resolve pylint error in nest api subscriber * Remove duplicate constants --- homeassistant/components/nest/__init__.py | 32 +++---------------- homeassistant/components/nest/api.py | 39 +++++++++++++++++++++-- homeassistant/components/nest/const.py | 4 +++ tests/components/nest/common.py | 3 +- tests/components/nest/test_init_sdm.py | 14 +++++--- 5 files changed, 57 insertions(+), 35 deletions(-) diff --git a/homeassistant/components/nest/__init__.py b/homeassistant/components/nest/__init__.py index 1cbe97dc3b7..37901d060e1 100644 --- a/homeassistant/components/nest/__init__.py +++ b/homeassistant/components/nest/__init__.py @@ -8,7 +8,6 @@ from google_nest_sdm.exceptions import ( ConfigurationException, GoogleNestException, ) -from google_nest_sdm.google_nest_subscriber import GoogleNestSubscriber import voluptuous as vol from homeassistant.config_entries import ConfigEntry @@ -22,15 +21,14 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from homeassistant.helpers import ( - aiohttp_client, - config_entry_oauth2_flow, - config_validation as cv, -) +from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv from homeassistant.helpers.typing import ConfigType from . import api, config_flow from .const import ( + CONF_PROJECT_ID, + CONF_SUBSCRIBER_ID, + DATA_NEST_CONFIG, DATA_SDM, DATA_SUBSCRIBER, DOMAIN, @@ -43,9 +41,6 @@ from .legacy import async_setup_legacy, async_setup_legacy_entry _LOGGER = logging.getLogger(__name__) -CONF_PROJECT_ID = "project_id" -CONF_SUBSCRIBER_ID = "subscriber_id" -DATA_NEST_CONFIG = "nest_config" DATA_NEST_UNAVAILABLE = "nest_unavailable" NEST_SETUP_NOTIFICATION = "nest_setup" @@ -199,24 +194,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: 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, - config[CONF_CLIENT_ID], - config[CONF_CLIENT_SECRET], - ) - subscriber = GoogleNestSubscriber( - auth, config[CONF_PROJECT_ID], config[CONF_SUBSCRIBER_ID] - ) + subscriber = await api.new_subscriber(hass, entry) callback = SignalUpdateCallback(hass) subscriber.set_update_callback(callback.async_handle_event) diff --git a/homeassistant/components/nest/api.py b/homeassistant/components/nest/api.py index 426a651461a..17b473dbeaa 100644 --- a/homeassistant/components/nest/api.py +++ b/homeassistant/components/nest/api.py @@ -6,10 +6,22 @@ from typing import cast from aiohttp import ClientSession from google.oauth2.credentials import Credentials from google_nest_sdm.auth import AbstractAuth +from google_nest_sdm.google_nest_subscriber import GoogleNestSubscriber -from homeassistant.helpers import config_entry_oauth2_flow +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET +from homeassistant.core import HomeAssistant +from homeassistant.helpers import aiohttp_client, config_entry_oauth2_flow -from .const import API_URL, OAUTH2_TOKEN, SDM_SCOPES +from .const import ( + API_URL, + CONF_PROJECT_ID, + CONF_SUBSCRIBER_ID, + DATA_NEST_CONFIG, + DOMAIN, + OAUTH2_TOKEN, + SDM_SCOPES, +) # See https://developers.google.com/nest/device-access/registration @@ -55,3 +67,26 @@ class AsyncConfigEntryAuth(AbstractAuth): ) creds.expiry = datetime.datetime.fromtimestamp(token["expires_at"]) return creds + + +async def new_subscriber( + hass: HomeAssistant, entry: ConfigEntry +) -> GoogleNestSubscriber: + """Create a GoogleNestSubscriber.""" + 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 = AsyncConfigEntryAuth( + aiohttp_client.async_get_clientsession(hass), + session, + config[CONF_CLIENT_ID], + config[CONF_CLIENT_SECRET], + ) + return GoogleNestSubscriber( + auth, config[CONF_PROJECT_ID], config[CONF_SUBSCRIBER_ID] + ) diff --git a/homeassistant/components/nest/const.py b/homeassistant/components/nest/const.py index 25b43de1032..6fcd74299ba 100644 --- a/homeassistant/components/nest/const.py +++ b/homeassistant/components/nest/const.py @@ -3,6 +3,10 @@ DOMAIN = "nest" DATA_SDM = "sdm" DATA_SUBSCRIBER = "subscriber" +DATA_NEST_CONFIG = "nest_config" + +CONF_PROJECT_ID = "project_id" +CONF_SUBSCRIBER_ID = "subscriber_id" SIGNAL_NEST_UPDATE = "nest_update" diff --git a/tests/components/nest/common.py b/tests/components/nest/common.py index d6dc730ec11..c9572c528bb 100644 --- a/tests/components/nest/common.py +++ b/tests/components/nest/common.py @@ -107,7 +107,8 @@ async def async_setup_sdm_platform(hass, platform, devices={}, structures={}): with patch( "homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation" ), patch("homeassistant.components.nest.PLATFORMS", [platform]), patch( - "homeassistant.components.nest.GoogleNestSubscriber", return_value=subscriber + "homeassistant.components.nest.api.GoogleNestSubscriber", + return_value=subscriber, ): assert await async_setup_component(hass, DOMAIN, CONFIG) await hass.async_block_till_done() diff --git a/tests/components/nest/test_init_sdm.py b/tests/components/nest/test_init_sdm.py index 205cc34fe20..59d1cbd0d69 100644 --- a/tests/components/nest/test_init_sdm.py +++ b/tests/components/nest/test_init_sdm.py @@ -60,7 +60,7 @@ async def test_setup_configuration_failure(hass, caplog): async def test_setup_susbcriber_failure(hass, caplog): """Test configuration error.""" with patch( - "homeassistant.components.nest.GoogleNestSubscriber.start_async", + "homeassistant.components.nest.api.GoogleNestSubscriber.start_async", side_effect=GoogleNestException(), ), caplog.at_level(logging.ERROR, logger="homeassistant.components.nest"): result = await async_setup_sdm(hass) @@ -74,10 +74,14 @@ async def test_setup_susbcriber_failure(hass, caplog): async def test_setup_device_manager_failure(hass, caplog): """Test configuration error.""" - with patch("homeassistant.components.nest.GoogleNestSubscriber.start_async"), patch( - "homeassistant.components.nest.GoogleNestSubscriber.async_get_device_manager", + with patch( + "homeassistant.components.nest.api.GoogleNestSubscriber.start_async" + ), patch( + "homeassistant.components.nest.api.GoogleNestSubscriber.async_get_device_manager", side_effect=GoogleNestException(), - ), caplog.at_level(logging.ERROR, logger="homeassistant.components.nest"): + ), caplog.at_level( + logging.ERROR, logger="homeassistant.components.nest" + ): result = await async_setup_sdm(hass) assert result assert len(caplog.messages) == 1 @@ -91,7 +95,7 @@ async def test_setup_device_manager_failure(hass, caplog): async def test_subscriber_auth_failure(hass, caplog): """Test configuration error.""" with patch( - "homeassistant.components.nest.GoogleNestSubscriber.start_async", + "homeassistant.components.nest.api.GoogleNestSubscriber.start_async", side_effect=AuthException(), ): result = await async_setup_sdm(hass, CONFIG)