From 3a3f70ef210ab594b00d8c91ab4cc3f829e5d3cf Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Wed, 31 Jul 2019 11:31:40 -0600 Subject: [PATCH] Add migration notification for Ambient PWS (#25561) * Add migration notification for Ambient PWS * Remove unnecessary kwarg Co-Authored-By: Andrew Sayre <6730289+andrewsayre@users.noreply.github.com> * Automatically recreate config entry * Delete entities and devices * Move imports --- .../components/ambient_station/__init__.py | 29 +++++++++++++++---- .../components/ambient_station/config_flow.py | 2 +- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/ambient_station/__init__.py b/homeassistant/components/ambient_station/__init__.py index d72302a624e..30028abb1c4 100644 --- a/homeassistant/components/ambient_station/__init__.py +++ b/homeassistant/components/ambient_station/__init__.py @@ -1,6 +1,8 @@ """Support for Ambient Weather Station Service.""" import logging +from aioambient import Client +from aioambient.errors import WebsocketError import voluptuous as vol from homeassistant.config_entries import SOURCE_IMPORT @@ -251,9 +253,6 @@ async def async_setup(hass, config): async def async_setup_entry(hass, config_entry): """Set up the Ambient PWS as config entry.""" - from aioambient import Client - from aioambient.errors import WebsocketError - session = aiohttp_client.async_get_clientsession(hass) try: @@ -286,6 +285,28 @@ async def async_unload_entry(hass, config_entry): return True +async def async_migrate_entry(hass, config_entry): + """Migrate old entry.""" + version = config_entry.version + + _LOGGER.debug('Migrating from version %s', version) + + # 1 -> 2: Unique ID format changed, so delete and re-import: + if version == 1: + dev_reg = await hass.helpers.device_registry.async_get_registry() + dev_reg.async_clear_config_entry(config_entry) + + en_reg = await hass.helpers.entity_registry.async_get_registry() + en_reg.async_clear_config_entry(config_entry) + + version = config_entry.version = 2 + hass.config_entries.async_update_entry(config_entry) + + _LOGGER.info('Migration to version %s successful', version) + + return True + + class AmbientStation: """Define a class to handle the Ambient websocket.""" @@ -302,8 +323,6 @@ class AmbientStation: async def _attempt_connect(self): """Attempt to connect to the socket (retrying later on fail).""" - from aioambient.errors import WebsocketError - try: await self.client.websocket.connect() except WebsocketError as err: diff --git a/homeassistant/components/ambient_station/config_flow.py b/homeassistant/components/ambient_station/config_flow.py index f01bfd8f791..895c140662f 100644 --- a/homeassistant/components/ambient_station/config_flow.py +++ b/homeassistant/components/ambient_station/config_flow.py @@ -21,7 +21,7 @@ def configured_instances(hass): class AmbientStationFlowHandler(config_entries.ConfigFlow): """Handle an Ambient PWS config flow.""" - VERSION = 1 + VERSION = 2 CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_PUSH async def _show_form(self, errors=None):