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
This commit is contained in:
Aaron Bach 2019-07-31 11:31:40 -06:00 committed by Paulus Schoutsen
parent 90dc81c1b3
commit 3a3f70ef21
2 changed files with 25 additions and 6 deletions

View File

@ -1,6 +1,8 @@
"""Support for Ambient Weather Station Service.""" """Support for Ambient Weather Station Service."""
import logging import logging
from aioambient import Client
from aioambient.errors import WebsocketError
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT 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): async def async_setup_entry(hass, config_entry):
"""Set up the Ambient PWS as 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) session = aiohttp_client.async_get_clientsession(hass)
try: try:
@ -286,6 +285,28 @@ async def async_unload_entry(hass, config_entry):
return True 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: class AmbientStation:
"""Define a class to handle the Ambient websocket.""" """Define a class to handle the Ambient websocket."""
@ -302,8 +323,6 @@ class AmbientStation:
async def _attempt_connect(self): async def _attempt_connect(self):
"""Attempt to connect to the socket (retrying later on fail).""" """Attempt to connect to the socket (retrying later on fail)."""
from aioambient.errors import WebsocketError
try: try:
await self.client.websocket.connect() await self.client.websocket.connect()
except WebsocketError as err: except WebsocketError as err:

View File

@ -21,7 +21,7 @@ def configured_instances(hass):
class AmbientStationFlowHandler(config_entries.ConfigFlow): class AmbientStationFlowHandler(config_entries.ConfigFlow):
"""Handle an Ambient PWS config flow.""" """Handle an Ambient PWS config flow."""
VERSION = 1 VERSION = 2
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_PUSH CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_PUSH
async def _show_form(self, errors=None): async def _show_form(self, errors=None):