From 61e093cecde1490c397c39943d6a44b839e78386 Mon Sep 17 00:00:00 2001 From: Aaron Bach <bachya1208@gmail.com> Date: Fri, 22 Oct 2021 04:17:25 -0600 Subject: [PATCH] Make sure Ambient PWS data storage conforms to standards (#57807) --- .../components/ambient_station/__init__.py | 14 ++++++++------ .../components/ambient_station/binary_sensor.py | 4 ++-- homeassistant/components/ambient_station/const.py | 2 -- homeassistant/components/ambient_station/sensor.py | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/ambient_station/__init__.py b/homeassistant/components/ambient_station/__init__.py index fc1f7d71d74..4e7e555ad94 100644 --- a/homeassistant/components/ambient_station/__init__.py +++ b/homeassistant/components/ambient_station/__init__.py @@ -26,7 +26,6 @@ from homeassistant.helpers.event import async_call_later from .const import ( ATTR_LAST_DATA, CONF_APP_KEY, - DATA_CLIENT, DOMAIN, LOGGER, TYPE_SOLARRADIATION, @@ -59,7 +58,8 @@ def async_hydrate_station_data(data: dict[str, Any]) -> dict[str, Any]: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up the Ambient PWS as config entry.""" - hass.data.setdefault(DOMAIN, {DATA_CLIENT: {}}) + hass.data.setdefault(DOMAIN, {}) + hass.data[DOMAIN][entry.entry_id] = {} if not entry.unique_id: hass.config_entries.async_update_entry( @@ -78,7 +78,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ), ) hass.loop.create_task(ambient.ws_connect()) - hass.data[DOMAIN][DATA_CLIENT][entry.entry_id] = ambient + hass.data[DOMAIN][entry.entry_id] = ambient except WebsocketError as err: LOGGER.error("Config entry failed: %s", err) raise ConfigEntryNotReady from err @@ -97,10 +97,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload an Ambient PWS config entry.""" - ambient = hass.data[DOMAIN][DATA_CLIENT].pop(entry.entry_id) - hass.async_create_task(ambient.ws_disconnect()) + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) + if unload_ok: + ambient = hass.data[DOMAIN].pop(entry.entry_id) + hass.async_create_task(ambient.ws_disconnect()) - return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) + return unload_ok async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/components/ambient_station/binary_sensor.py b/homeassistant/components/ambient_station/binary_sensor.py index 79c53246f15..0c819a9a9b7 100644 --- a/homeassistant/components/ambient_station/binary_sensor.py +++ b/homeassistant/components/ambient_station/binary_sensor.py @@ -16,7 +16,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import AmbientWeatherEntity -from .const import ATTR_LAST_DATA, DATA_CLIENT, DOMAIN +from .const import ATTR_LAST_DATA, DOMAIN TYPE_BATT1 = "batt1" TYPE_BATT10 = "batt10" @@ -234,7 +234,7 @@ async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up Ambient PWS binary sensors based on a config entry.""" - ambient = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id] + ambient = hass.data[DOMAIN][entry.entry_id] async_add_entities( [ diff --git a/homeassistant/components/ambient_station/const.py b/homeassistant/components/ambient_station/const.py index cf5c97be045..4e0ec598fb1 100644 --- a/homeassistant/components/ambient_station/const.py +++ b/homeassistant/components/ambient_station/const.py @@ -8,7 +8,5 @@ ATTR_LAST_DATA = "last_data" CONF_APP_KEY = "app_key" -DATA_CLIENT = "data_client" - TYPE_SOLARRADIATION = "solarradiation" TYPE_SOLARRADIATION_LX = "solarradiation_lx" diff --git a/homeassistant/components/ambient_station/sensor.py b/homeassistant/components/ambient_station/sensor.py index 0247d03b6fd..66bccb30b55 100644 --- a/homeassistant/components/ambient_station/sensor.py +++ b/homeassistant/components/ambient_station/sensor.py @@ -39,7 +39,7 @@ from . import ( AmbientStation, AmbientWeatherEntity, ) -from .const import ATTR_LAST_DATA, DATA_CLIENT, DOMAIN +from .const import ATTR_LAST_DATA, DOMAIN TYPE_24HOURRAININ = "24hourrainin" TYPE_BAROMABSIN = "baromabsin" @@ -609,7 +609,7 @@ async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up Ambient PWS sensors based on a config entry.""" - ambient = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id] + ambient = hass.data[DOMAIN][entry.entry_id] async_add_entities( [