mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
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
This commit is contained in:
parent
9c2bff3b3b
commit
0991a30125
@ -8,7 +8,6 @@ from google_nest_sdm.exceptions import (
|
|||||||
ConfigurationException,
|
ConfigurationException,
|
||||||
GoogleNestException,
|
GoogleNestException,
|
||||||
)
|
)
|
||||||
from google_nest_sdm.google_nest_subscriber import GoogleNestSubscriber
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -22,15 +21,14 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers import (
|
from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv
|
||||||
aiohttp_client,
|
|
||||||
config_entry_oauth2_flow,
|
|
||||||
config_validation as cv,
|
|
||||||
)
|
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
from . import api, config_flow
|
from . import api, config_flow
|
||||||
from .const import (
|
from .const import (
|
||||||
|
CONF_PROJECT_ID,
|
||||||
|
CONF_SUBSCRIBER_ID,
|
||||||
|
DATA_NEST_CONFIG,
|
||||||
DATA_SDM,
|
DATA_SDM,
|
||||||
DATA_SUBSCRIBER,
|
DATA_SUBSCRIBER,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
@ -43,9 +41,6 @@ from .legacy import async_setup_legacy, async_setup_legacy_entry
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONF_PROJECT_ID = "project_id"
|
|
||||||
CONF_SUBSCRIBER_ID = "subscriber_id"
|
|
||||||
DATA_NEST_CONFIG = "nest_config"
|
|
||||||
DATA_NEST_UNAVAILABLE = "nest_unavailable"
|
DATA_NEST_UNAVAILABLE = "nest_unavailable"
|
||||||
|
|
||||||
NEST_SETUP_NOTIFICATION = "nest_setup"
|
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:
|
if DATA_SDM not in entry.data:
|
||||||
return await async_setup_legacy_entry(hass, entry)
|
return await async_setup_legacy_entry(hass, entry)
|
||||||
|
|
||||||
implementation = (
|
subscriber = await api.new_subscriber(hass, entry)
|
||||||
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]
|
|
||||||
)
|
|
||||||
callback = SignalUpdateCallback(hass)
|
callback = SignalUpdateCallback(hass)
|
||||||
subscriber.set_update_callback(callback.async_handle_event)
|
subscriber.set_update_callback(callback.async_handle_event)
|
||||||
|
|
||||||
|
@ -6,10 +6,22 @@ from typing import cast
|
|||||||
from aiohttp import ClientSession
|
from aiohttp import ClientSession
|
||||||
from google.oauth2.credentials import Credentials
|
from google.oauth2.credentials import Credentials
|
||||||
from google_nest_sdm.auth import AbstractAuth
|
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
|
# See https://developers.google.com/nest/device-access/registration
|
||||||
|
|
||||||
@ -55,3 +67,26 @@ class AsyncConfigEntryAuth(AbstractAuth):
|
|||||||
)
|
)
|
||||||
creds.expiry = datetime.datetime.fromtimestamp(token["expires_at"])
|
creds.expiry = datetime.datetime.fromtimestamp(token["expires_at"])
|
||||||
return creds
|
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]
|
||||||
|
)
|
||||||
|
@ -3,6 +3,10 @@
|
|||||||
DOMAIN = "nest"
|
DOMAIN = "nest"
|
||||||
DATA_SDM = "sdm"
|
DATA_SDM = "sdm"
|
||||||
DATA_SUBSCRIBER = "subscriber"
|
DATA_SUBSCRIBER = "subscriber"
|
||||||
|
DATA_NEST_CONFIG = "nest_config"
|
||||||
|
|
||||||
|
CONF_PROJECT_ID = "project_id"
|
||||||
|
CONF_SUBSCRIBER_ID = "subscriber_id"
|
||||||
|
|
||||||
SIGNAL_NEST_UPDATE = "nest_update"
|
SIGNAL_NEST_UPDATE = "nest_update"
|
||||||
|
|
||||||
|
@ -107,7 +107,8 @@ async def async_setup_sdm_platform(hass, platform, devices={}, structures={}):
|
|||||||
with patch(
|
with patch(
|
||||||
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation"
|
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation"
|
||||||
), patch("homeassistant.components.nest.PLATFORMS", [platform]), patch(
|
), 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)
|
assert await async_setup_component(hass, DOMAIN, CONFIG)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
@ -60,7 +60,7 @@ async def test_setup_configuration_failure(hass, caplog):
|
|||||||
async def test_setup_susbcriber_failure(hass, caplog):
|
async def test_setup_susbcriber_failure(hass, caplog):
|
||||||
"""Test configuration error."""
|
"""Test configuration error."""
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.nest.GoogleNestSubscriber.start_async",
|
"homeassistant.components.nest.api.GoogleNestSubscriber.start_async",
|
||||||
side_effect=GoogleNestException(),
|
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)
|
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):
|
async def test_setup_device_manager_failure(hass, caplog):
|
||||||
"""Test configuration error."""
|
"""Test configuration error."""
|
||||||
with patch("homeassistant.components.nest.GoogleNestSubscriber.start_async"), patch(
|
with patch(
|
||||||
"homeassistant.components.nest.GoogleNestSubscriber.async_get_device_manager",
|
"homeassistant.components.nest.api.GoogleNestSubscriber.start_async"
|
||||||
|
), patch(
|
||||||
|
"homeassistant.components.nest.api.GoogleNestSubscriber.async_get_device_manager",
|
||||||
side_effect=GoogleNestException(),
|
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)
|
result = await async_setup_sdm(hass)
|
||||||
assert result
|
assert result
|
||||||
assert len(caplog.messages) == 1
|
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):
|
async def test_subscriber_auth_failure(hass, caplog):
|
||||||
"""Test configuration error."""
|
"""Test configuration error."""
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.nest.GoogleNestSubscriber.start_async",
|
"homeassistant.components.nest.api.GoogleNestSubscriber.start_async",
|
||||||
side_effect=AuthException(),
|
side_effect=AuthException(),
|
||||||
):
|
):
|
||||||
result = await async_setup_sdm(hass, CONFIG)
|
result = await async_setup_sdm(hass, CONFIG)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user