2020-10-27 14:20:01 +00:00
|
|
|
"""Common libraries for test setup."""
|
|
|
|
|
|
|
|
import time
|
2020-12-27 08:49:22 +00:00
|
|
|
from typing import Awaitable, Callable
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2020-10-27 14:20:01 +00:00
|
|
|
|
|
|
|
from google_nest_sdm.device_manager import DeviceManager
|
2020-12-27 08:49:22 +00:00
|
|
|
from google_nest_sdm.event import EventMessage
|
2021-12-05 17:45:40 +00:00
|
|
|
from google_nest_sdm.event_media import CachePolicy
|
2020-10-27 14:20:01 +00:00
|
|
|
from google_nest_sdm.google_nest_subscriber import GoogleNestSubscriber
|
|
|
|
|
|
|
|
from homeassistant.components.nest import DOMAIN
|
2021-01-02 00:51:01 +00:00
|
|
|
from homeassistant.components.nest.const import SDM_SCOPES
|
2020-10-27 14:20:01 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
2021-01-02 00:51:01 +00:00
|
|
|
PROJECT_ID = "some-project-id"
|
|
|
|
CLIENT_ID = "some-client-id"
|
|
|
|
CLIENT_SECRET = "some-client-secret"
|
|
|
|
|
2020-10-27 14:20:01 +00:00
|
|
|
CONFIG = {
|
|
|
|
"nest": {
|
2021-01-02 00:51:01 +00:00
|
|
|
"client_id": CLIENT_ID,
|
|
|
|
"client_secret": CLIENT_SECRET,
|
2020-10-27 14:20:01 +00:00
|
|
|
# Required fields for using SDM API
|
2021-01-02 00:51:01 +00:00
|
|
|
"project_id": PROJECT_ID,
|
2020-12-28 04:30:51 +00:00
|
|
|
"subscriber_id": "projects/example/subscriptions/subscriber-id-9876",
|
2020-10-27 14:20:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-01-02 00:51:01 +00:00
|
|
|
FAKE_TOKEN = "some-token"
|
|
|
|
FAKE_REFRESH_TOKEN = "some-refresh-token"
|
|
|
|
|
|
|
|
|
2021-11-30 06:41:29 +00:00
|
|
|
def create_config_entry(hass, token_expiration_time=None) -> MockConfigEntry:
|
2021-01-02 00:51:01 +00:00
|
|
|
"""Create a ConfigEntry and add it to Home Assistant."""
|
|
|
|
if token_expiration_time is None:
|
|
|
|
token_expiration_time = time.time() + 86400
|
|
|
|
config_entry_data = {
|
|
|
|
"sdm": {}, # Indicates new SDM API, not legacy API
|
|
|
|
"auth_implementation": "nest",
|
|
|
|
"token": {
|
|
|
|
"access_token": FAKE_TOKEN,
|
|
|
|
"refresh_token": FAKE_REFRESH_TOKEN,
|
|
|
|
"scope": " ".join(SDM_SCOPES),
|
|
|
|
"token_type": "Bearer",
|
|
|
|
"expires_at": token_expiration_time,
|
2020-10-27 14:20:01 +00:00
|
|
|
},
|
2021-01-02 00:51:01 +00:00
|
|
|
}
|
2021-11-30 06:41:29 +00:00
|
|
|
config_entry = MockConfigEntry(domain=DOMAIN, data=config_entry_data)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
return config_entry
|
2020-10-27 14:20:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FakeSubscriber(GoogleNestSubscriber):
|
|
|
|
"""Fake subscriber that supplies a FakeDeviceManager."""
|
|
|
|
|
2021-12-05 21:02:37 +00:00
|
|
|
def __init__(self):
|
2020-10-27 14:20:01 +00:00
|
|
|
"""Initialize Fake Subscriber."""
|
2021-12-05 21:02:37 +00:00
|
|
|
self._device_manager = DeviceManager()
|
2020-10-27 14:20:01 +00:00
|
|
|
|
2020-12-27 08:49:22 +00:00
|
|
|
def set_update_callback(self, callback: Callable[[EventMessage], Awaitable[None]]):
|
2020-10-27 14:20:01 +00:00
|
|
|
"""Capture the callback set by Home Assistant."""
|
2021-12-12 22:12:05 +00:00
|
|
|
self._device_manager.set_update_callback(callback)
|
2020-10-27 14:20:01 +00:00
|
|
|
|
2021-11-30 06:41:29 +00:00
|
|
|
async def create_subscription(self):
|
|
|
|
"""Create the subscription."""
|
|
|
|
return
|
|
|
|
|
|
|
|
async def delete_subscription(self):
|
|
|
|
"""Delete the subscription."""
|
|
|
|
return
|
|
|
|
|
2020-12-28 04:30:51 +00:00
|
|
|
async def start_async(self):
|
2020-10-27 14:20:01 +00:00
|
|
|
"""Return the fake device manager."""
|
|
|
|
return self._device_manager
|
|
|
|
|
|
|
|
async def async_get_device_manager(self) -> DeviceManager:
|
|
|
|
"""Return the fake device manager."""
|
|
|
|
return self._device_manager
|
|
|
|
|
2021-12-05 17:45:40 +00:00
|
|
|
@property
|
|
|
|
def cache_policy(self) -> CachePolicy:
|
|
|
|
"""Return the cache policy."""
|
|
|
|
return self._device_manager.cache_policy
|
|
|
|
|
2020-10-27 14:20:01 +00:00
|
|
|
def stop_async(self):
|
|
|
|
"""No-op to stop the subscriber."""
|
|
|
|
return None
|
|
|
|
|
2020-11-24 15:53:50 +00:00
|
|
|
async def async_receive_event(self, event_message: EventMessage):
|
2020-10-27 14:20:01 +00:00
|
|
|
"""Simulate a received pubsub message, invoked by tests."""
|
|
|
|
# Update device state, then invoke HomeAssistant to refresh
|
2020-11-24 15:53:50 +00:00
|
|
|
await self._device_manager.async_handle_event(event_message)
|
2020-10-27 14:20:01 +00:00
|
|
|
|
|
|
|
|
2021-11-30 06:41:29 +00:00
|
|
|
async def async_setup_sdm_platform(
|
|
|
|
hass, platform, devices={}, structures={}, with_config=True
|
|
|
|
):
|
2020-10-27 14:20:01 +00:00
|
|
|
"""Set up the platform and prerequisites."""
|
2021-11-30 06:41:29 +00:00
|
|
|
if with_config:
|
|
|
|
create_config_entry(hass)
|
2021-12-05 21:02:37 +00:00
|
|
|
subscriber = FakeSubscriber()
|
|
|
|
device_manager = await subscriber.async_get_device_manager()
|
|
|
|
if devices:
|
|
|
|
for device in devices.values():
|
|
|
|
device_manager.add_device(device)
|
|
|
|
if structures:
|
|
|
|
for structure in structures.values():
|
|
|
|
device_manager.add_structure(structure)
|
2020-10-27 14:20:01 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation"
|
|
|
|
), patch("homeassistant.components.nest.PLATFORMS", [platform]), patch(
|
2021-11-15 00:08:22 +00:00
|
|
|
"homeassistant.components.nest.api.GoogleNestSubscriber",
|
|
|
|
return_value=subscriber,
|
2020-10-27 14:20:01 +00:00
|
|
|
):
|
|
|
|
assert await async_setup_component(hass, DOMAIN, CONFIG)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
return subscriber
|