Update nest config flow to dramatically simplify end user setup with automated pub/sub subscription creation (#59260)

* Configure nest pubsub subscriber automatically

Update the config flow to configure the nest pubsub subscriber automatically.
After completing the authentication step, the user is now asked for the google
cloud console ID, which is needed to create a subscription.

Home Assistant manages the lifecycle of a subscription only when it is created
by the ConfigFlow. Otherwise (if specified in configuration.yaml) it treats
it similarly as before.

These are the considerations or failure modes taken into account:
- Subscription is created with reasonable default values as previously recommended (e.g. retion only keeps 5-15 minutes of backlog messages)
- Subscriptions are created with a naming scheme that makes it clear they came from home assistant, and with a random
  string
- Subscriptions are cleaned up when the ConfigEntry is removed. If removal fails, a subscription that is orphaned will
  be deleted after 30 days
- If the subscription gets into a bad state or deleted, the user can go through the re-auth flow to re-create it.
- Users can still specifcy a CONF_SUBSCRIBER_ID in the configuration.yaml, and
skip automatic subscriber creation

* Remove unnecessary nest config flow diffs and merge in upstream changes

* Incorporate review feedback into nest subscription config flow

* Update text wording in nest config flow
This commit is contained in:
Allen Porter 2021-11-29 22:41:29 -08:00 committed by GitHub
parent 8ca89b10eb
commit cc543b200d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 658 additions and 99 deletions

View file

@ -32,7 +32,7 @@ FAKE_TOKEN = "some-token"
FAKE_REFRESH_TOKEN = "some-refresh-token"
def create_config_entry(hass, token_expiration_time=None):
def create_config_entry(hass, token_expiration_time=None) -> MockConfigEntry:
"""Create a ConfigEntry and add it to Home Assistant."""
if token_expiration_time is None:
token_expiration_time = time.time() + 86400
@ -47,7 +47,9 @@ def create_config_entry(hass, token_expiration_time=None):
"expires_at": token_expiration_time,
},
}
MockConfigEntry(domain=DOMAIN, data=config_entry_data).add_to_hass(hass)
config_entry = MockConfigEntry(domain=DOMAIN, data=config_entry_data)
config_entry.add_to_hass(hass)
return config_entry
class FakeDeviceManager(DeviceManager):
@ -80,6 +82,14 @@ class FakeSubscriber(GoogleNestSubscriber):
"""Capture the callback set by Home Assistant."""
self._callback = callback
async def create_subscription(self):
"""Create the subscription."""
return
async def delete_subscription(self):
"""Delete the subscription."""
return
async def start_async(self):
"""Return the fake device manager."""
return self._device_manager
@ -99,9 +109,12 @@ class FakeSubscriber(GoogleNestSubscriber):
await self._callback(event_message)
async def async_setup_sdm_platform(hass, platform, devices={}, structures={}):
async def async_setup_sdm_platform(
hass, platform, devices={}, structures={}, with_config=True
):
"""Set up the platform and prerequisites."""
create_config_entry(hass)
if with_config:
create_config_entry(hass)
device_manager = FakeDeviceManager(devices=devices, structures=structures)
subscriber = FakeSubscriber(device_manager)
with patch(