Update Nest integration to support Google Nest Device Access (new API) (#41689)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
1 parent
9bc0509f28
commit
52b66e88c7
17 files changed
+1001
-230
No files matched your search
@@ -0,0 +1,248 @@
|
||||
"""
|
||||
Test for Nest sensors platform for the Smart Device Management API.
|
||||
|
||||
These tests fake out the subscriber/devicemanager, and are not using a real
|
||||
pubsub subscriber.
|
||||
"""
|
||||
|
||||
import time
|
||||
|
||||
from google_nest_sdm.device import Device
|
||||
from google_nest_sdm.device_manager import DeviceManager
|
||||
from google_nest_sdm.event import EventCallback, EventMessage
|
||||
from google_nest_sdm.google_nest_subscriber import GoogleNestSubscriber
|
||||
|
||||
from homeassistant.components.nest import DOMAIN
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
PLATFORM = "sensor"
|
||||
|
||||
CONFIG = {
|
||||
"nest": {
|
||||
"client_id": "some-client-id",
|
||||
"client_secret": "some-client-secret",
|
||||
# Required fields for using SDM API
|
||||
"project_id": "some-project-id",
|
||||
"subscriber_id": "some-subscriber-id",
|
||||
},
|
||||
}
|
||||
|
||||
CONFIG_ENTRY_DATA = {
|
||||
"sdm": {}, # Indicates new SDM API, not legacy API
|
||||
"auth_implementation": "local",
|
||||
"token": {
|
||||
"expires_at": time.time() + 86400,
|
||||
"access_token": {
|
||||
"token": "some-token",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class FakeDeviceManager(DeviceManager):
|
||||
"""Fake DeviceManager that can supply a list of devices and structures."""
|
||||
|
||||
def __init__(self, devices: dict, structures: dict):
|
||||
"""Initialize FakeDeviceManager."""
|
||||
super().__init__()
|
||||
self._devices = devices
|
||||
|
||||
@property
|
||||
def structures(self) -> dict:
|
||||
"""Override structures with fake result."""
|
||||
return self._structures
|
||||
|
||||
@property
|
||||
def devices(self) -> dict:
|
||||
"""Override devices with fake result."""
|
||||
return self._devices
|
||||
|
||||
|
||||
class FakeSubscriber(GoogleNestSubscriber):
|
||||
"""Fake subscriber that supplies a FakeDeviceManager."""
|
||||
|
||||
def __init__(self, device_manager: FakeDeviceManager):
|
||||
"""Initialize Fake Subscriber."""
|
||||
self._device_manager = device_manager
|
||||
self._callback = None
|
||||
|
||||
def set_update_callback(self, callback: EventCallback):
|
||||
"""Capture the callback set by Home Assistant."""
|
||||
self._callback = callback
|
||||
|
||||
async def start_async(self) -> DeviceManager:
|
||||
"""Return the fake device manager."""
|
||||
return self._device_manager
|
||||
|
||||
@property
|
||||
async def async_device_manager(self) -> DeviceManager:
|
||||
"""Return the fake device manager."""
|
||||
return self._device_manager
|
||||
|
||||
def stop_async(self):
|
||||
"""No-op to stop the subscriber."""
|
||||
return None
|
||||
|
||||
def receive_event(self, event_message: EventMessage):
|
||||
"""Simulate a received pubsub message, invoked by tests."""
|
||||
# Update device state, then invoke HomeAssistant to refresh
|
||||
self._device_manager.handle_event(event_message)
|
||||
self._callback.handle_event(event_message)
|
||||
|
||||
|
||||
async def setup_sensor(hass, devices={}, structures={}):
|
||||
"""Set up the platform and prerequisites."""
|
||||
MockConfigEntry(domain=DOMAIN, data=CONFIG_ENTRY_DATA).add_to_hass(hass)
|
||||
device_manager = FakeDeviceManager(devices=devices, structures=structures)
|
||||
subscriber = FakeSubscriber(device_manager)
|
||||
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
|
||||
):
|
||||
assert await async_setup_component(hass, DOMAIN, CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
return subscriber
|
||||
|
||||
|
||||
async def test_thermostat_device(hass):
|
||||
"""Test a thermostat with temperature and humidity sensors."""
|
||||
devices = {
|
||||
"some-device-id": Device.MakeDevice(
|
||||
{
|
||||
"name": "some-device-id",
|
||||
"type": "sdm.devices.types.Thermostat",
|
||||
"traits": {
|
||||
"sdm.devices.traits.Info": {
|
||||
"customName": "My Sensor",
|
||||
},
|
||||
"sdm.devices.traits.Temperature": {
|
||||
"ambientTemperatureCelsius": 25.1,
|
||||
},
|
||||
"sdm.devices.traits.Humidity": {
|
||||
"ambientHumidityPercent": 35.0,
|
||||
},
|
||||
},
|
||||
},
|
||||
auth=None,
|
||||
)
|
||||
}
|
||||
await setup_sensor(hass, devices)
|
||||
|
||||
temperature = hass.states.get("sensor.my_sensor_temperature")
|
||||
assert temperature is not None
|
||||
assert temperature.state == "25.1"
|
||||
|
||||
humidity = hass.states.get("sensor.my_sensor_humidity")
|
||||
assert humidity is not None
|
||||
assert humidity.state == "35.0"
|
||||
|
||||
|
||||
async def test_no_devices(hass):
|
||||
"""Test no devices returned by the api."""
|
||||
await setup_sensor(hass)
|
||||
|
||||
temperature = hass.states.get("sensor.my_sensor_temperature")
|
||||
assert temperature is None
|
||||
|
||||
humidity = hass.states.get("sensor.my_sensor_humidity")
|
||||
assert humidity is None
|
||||
|
||||
|
||||
async def test_device_no_sensor_traits(hass):
|
||||
"""Test a device with applicable sensor traits."""
|
||||
devices = {
|
||||
"some-device-id": Device.MakeDevice(
|
||||
{
|
||||
"name": "some-device-id",
|
||||
"type": "sdm.devices.types.Thermostat",
|
||||
"traits": {},
|
||||
},
|
||||
auth=None,
|
||||
)
|
||||
}
|
||||
await setup_sensor(hass, devices)
|
||||
|
||||
temperature = hass.states.get("sensor.my_sensor_temperature")
|
||||
assert temperature is None
|
||||
|
||||
humidity = hass.states.get("sensor.my_sensor_humidity")
|
||||
assert humidity is None
|
||||
|
||||
|
||||
async def test_device_name_from_structure(hass):
|
||||
"""Test a device without a custom name, inferring name from structure."""
|
||||
devices = {
|
||||
"some-device-id": Device.MakeDevice(
|
||||
{
|
||||
"name": "some-device-id",
|
||||
"type": "sdm.devices.types.Thermostat",
|
||||
"traits": {
|
||||
"sdm.devices.traits.Temperature": {
|
||||
"ambientTemperatureCelsius": 25.2,
|
||||
},
|
||||
},
|
||||
"parentRelations": [
|
||||
{"parent": "some-structure-id", "displayName": "Some Room"}
|
||||
],
|
||||
},
|
||||
auth=None,
|
||||
)
|
||||
}
|
||||
await setup_sensor(hass, devices)
|
||||
|
||||
temperature = hass.states.get("sensor.some_room_temperature")
|
||||
assert temperature is not None
|
||||
assert temperature.state == "25.2"
|
||||
|
||||
|
||||
async def test_event_updates_sensor(hass):
|
||||
"""Test a pubsub message received by subscriber to update temperature."""
|
||||
devices = {
|
||||
"some-device-id": Device.MakeDevice(
|
||||
{
|
||||
"name": "some-device-id",
|
||||
"type": "sdm.devices.types.Thermostat",
|
||||
"traits": {
|
||||
"sdm.devices.traits.Info": {
|
||||
"customName": "My Sensor",
|
||||
},
|
||||
"sdm.devices.traits.Temperature": {
|
||||
"ambientTemperatureCelsius": 25.1,
|
||||
},
|
||||
},
|
||||
},
|
||||
auth=None,
|
||||
)
|
||||
}
|
||||
subscriber = await setup_sensor(hass, devices)
|
||||
|
||||
temperature = hass.states.get("sensor.my_sensor_temperature")
|
||||
assert temperature is not None
|
||||
assert temperature.state == "25.1"
|
||||
|
||||
# Simulate a pubsub message received by the subscriber with a trait update
|
||||
event = EventMessage(
|
||||
{
|
||||
"eventId": "some-event-id",
|
||||
"timestamp": "2019-01-01T00:00:01Z",
|
||||
"resourceUpdate": {
|
||||
"name": "some-device-id",
|
||||
"traits": {
|
||||
"sdm.devices.traits.Temperature": {
|
||||
"ambientTemperatureCelsius": 26.2,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
auth=None,
|
||||
)
|
||||
subscriber.receive_event(event)
|
||||
await hass.async_block_till_done() # Process dispatch/update signal
|
||||
|
||||
temperature = hass.states.get("sensor.my_sensor_temperature")
|
||||
assert temperature is not None
|
||||
assert temperature.state == "26.2"
|
||||
File renamed without changes.
@@ -0,0 +1,66 @@
|
||||
"""Test the Google Nest Device Access config flow."""
|
||||
from homeassistant import config_entries, setup
|
||||
from homeassistant.components.nest.const import DOMAIN, OAUTH2_AUTHORIZE, OAUTH2_TOKEN
|
||||
from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET
|
||||
from homeassistant.helpers import config_entry_oauth2_flow
|
||||
|
||||
from tests.async_mock import patch
|
||||
|
||||
CLIENT_ID = "1234"
|
||||
CLIENT_SECRET = "5678"
|
||||
PROJECT_ID = "project-id-4321"
|
||||
SUBSCRIBER_ID = "subscriber-id-9876"
|
||||
|
||||
|
||||
async def test_full_flow(hass, aiohttp_client, aioclient_mock, current_request):
|
||||
"""Check full flow."""
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{
|
||||
DOMAIN: {
|
||||
"project_id": PROJECT_ID,
|
||||
"subscriber_id": SUBSCRIBER_ID,
|
||||
CONF_CLIENT_ID: CLIENT_ID,
|
||||
CONF_CLIENT_SECRET: CLIENT_SECRET,
|
||||
},
|
||||
"http": {"base_url": "https://example.com"},
|
||||
},
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
state = config_entry_oauth2_flow._encode_jwt(hass, {"flow_id": result["flow_id"]})
|
||||
|
||||
oauth_authorize = OAUTH2_AUTHORIZE.format(project_id=PROJECT_ID)
|
||||
assert result["url"] == (
|
||||
f"{oauth_authorize}?response_type=code&client_id={CLIENT_ID}"
|
||||
"&redirect_uri=https://example.com/auth/external/callback"
|
||||
f"&state={state}&scope=https://www.googleapis.com/auth/sdm.service"
|
||||
"+https://www.googleapis.com/auth/pubsub"
|
||||
"&access_type=offline&prompt=consent"
|
||||
)
|
||||
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
resp = await client.get(f"/auth/external/callback?code=abcd&state={state}")
|
||||
assert resp.status == 200
|
||||
assert resp.headers["content-type"] == "text/html; charset=utf-8"
|
||||
|
||||
aioclient_mock.post(
|
||||
OAUTH2_TOKEN,
|
||||
json={
|
||||
"refresh_token": "mock-refresh-token",
|
||||
"access_token": "mock-access-token",
|
||||
"type": "Bearer",
|
||||
"expires_in": 60,
|
||||
},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nest.async_setup_entry", return_value=True
|
||||
) as mock_setup:
|
||||
await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
Reference in new issue
Block a user