From 113db9afd47e5db905940a82715033a0be24018b Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Fri, 8 Mar 2019 20:25:35 -0700 Subject: [PATCH] Fix config entry exception in Ambient PWS (#21836) --- .../components/ambient_station/__init__.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/ambient_station/__init__.py b/homeassistant/components/ambient_station/__init__.py index 70f6ce9fbba..c7b001121da 100644 --- a/homeassistant/components/ambient_station/__init__.py +++ b/homeassistant/components/ambient_station/__init__.py @@ -296,6 +296,7 @@ class AmbientStation: def __init__(self, hass, config_entry, client, monitored_conditions): """Initialize.""" self._config_entry = config_entry + self._entry_setup_complete = False self._hass = hass self._watchdog_listener = None self._ws_reconnect_delay = DEFAULT_SOCKET_MIN_RETRY @@ -362,12 +363,18 @@ class AmbientStation: 'name', station['macAddress']), } - for component in ('binary_sensor', 'sensor'): - self._hass.async_create_task( - self._hass.config_entries.async_forward_entry_setup( - self._config_entry, component)) + # If the websocket disconnects and reconnects, the on_subscribed + # hanlder will get called again; in that case, we don't want to + # attempt forward setup of the config entry (because it will have + # already been done): + if not self._entry_setup_complete: + for component in ('binary_sensor', 'sensor'): + self._hass.async_create_task( + self._hass.config_entries.async_forward_entry_setup( + self._config_entry, component)) + self._entry_setup_complete = True - self._ws_reconnect_delay = DEFAULT_SOCKET_MIN_RETRY + self._ws_reconnect_delay = DEFAULT_SOCKET_MIN_RETRY self.client.websocket.on_connect(on_connect) self.client.websocket.on_data(on_data)