SmartThings remove SmartApp/Automation on integration removal (#21594)

* Add clean-up logic upon entry removal

* Removed unecessary app removal from migration

* Change log level and clarified code
This commit is contained in:
Andrew Sayre authored and GitHub committed 2019-03-03 13:47:25 -06:00
1 parent 0f6e0aa355
commit 3e0459cef9
3 files changed
+155 -21

No files matched your search

@@ -46,23 +46,7 @@ async def async_migrate_entry(hass: HomeAssistantType, entry: ConfigEntry):
integration setup again so we can properly retrieve the needed data
elements. Force this by removing the entry and triggering a new flow.
"""
from pysmartthings import SmartThings
# Remove the installed_app, which if already removed raises a 403 error.
api = SmartThings(async_get_clientsession(hass),
entry.data[CONF_ACCESS_TOKEN])
installed_app_id = entry.data[CONF_INSTALLED_APP_ID]
try:
await api.delete_installed_app(installed_app_id)
except ClientResponseError as ex:
if ex.status == 403:
_LOGGER.exception("Installed app %s has already been removed",
installed_app_id)
else:
raise
_LOGGER.debug("Removed installed app %s", installed_app_id)
# Delete the entry
# Remove the entry which will invoke the callback to delete the app.
hass.async_create_task(
hass.config_entries.async_remove(entry.entry_id))
# only create new flow if there isn't a pending one for SmartThings.
@@ -194,6 +178,47 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):
return all(await asyncio.gather(*tasks))
async def async_remove_entry(
hass: HomeAssistantType, entry: ConfigEntry) -> None:
"""Perform clean-up when entry is being removed."""
from pysmartthings import SmartThings
api = SmartThings(async_get_clientsession(hass),
entry.data[CONF_ACCESS_TOKEN])
# Remove the installed_app, which if already removed raises a 403 error.
installed_app_id = entry.data[CONF_INSTALLED_APP_ID]
try:
await api.delete_installed_app(installed_app_id)
except ClientResponseError as ex:
if ex.status == 403:
_LOGGER.debug("Installed app %s has already been removed",
installed_app_id, exc_info=True)
else:
raise
_LOGGER.debug("Removed installed app %s", installed_app_id)
# Remove the app if not referenced by other entries, which if already
# removed raises a 403 error.
app_id = entry.data[CONF_APP_ID]
app_count = sum(1 for entry in hass.config_entries.async_entries(DOMAIN)
if entry.data[CONF_APP_ID] == app_id)
if app_count > 1:
_LOGGER.debug("App %s was not removed because it is in use by other"
"config entries", app_id)
return
# Remove the app
try:
await api.delete_app(app_id)
except ClientResponseError as ex:
if ex.status == 403:
_LOGGER.debug("App %s has already been removed",
app_id, exc_info=True)
else:
raise
_LOGGER.debug("Removed app %s", app_id)
class DeviceBroker:
"""Manages an individual SmartThings config entry."""
@@ -5,7 +5,7 @@ from typing import Optional, Sequence
from homeassistant.const import (
DEVICE_CLASS_BATTERY, DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TIMESTAMP, MASS_KILOGRAMS,
TEMP_CELSIUS, TEMP_FAHRENHEIT, POWER_WATT)
POWER_WATT, TEMP_CELSIUS, TEMP_FAHRENHEIT)
from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN