From 2d1919896782918e018516916d944eca54eee62b Mon Sep 17 00:00:00 2001 From: Jaco Kok Date: Mon, 24 May 2021 22:18:17 +0000 Subject: [PATCH] New add_update_listener to update config. Cleaned up more code --- custom_components/warzone/__init__.py | 19 ++++++++++--------- custom_components/warzone/config_flow.py | 5 +---- custom_components/warzone/sensor.py | 1 + 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/custom_components/warzone/__init__.py b/custom_components/warzone/__init__.py index 021b314..d36eeec 100644 --- a/custom_components/warzone/__init__.py +++ b/custom_components/warzone/__init__.py @@ -3,11 +3,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -import logging -from .const import CONF_PASSWORD, CONF_USERNAME, DOMAIN -from .lib import Login - -_LOGGER = logging.getLogger(__name__) +from .const import DOMAIN PLATFORMS = ["sensor"] @@ -17,13 +13,18 @@ async def async_setup(hass: HomeAssistant, config: dict): async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up warzone from a config entry.""" - hass.data.setdefault(DOMAIN, {}) + if hass.data.get(DOMAIN) is None: + hass.data.setdefault(DOMAIN, {}) hass.config_entries.async_setup_platforms(entry, PLATFORMS) + if not entry.update_listeners: + entry.add_update_listener(async_reload_entry) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) - if unload_ok: - hass.data[DOMAIN].pop(entry.entry_id) - return unload_ok \ No newline at end of file + return unload_ok + +async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry): + """Reload config entry.""" + await hass.config_entries.async_reload(entry.entry_id) \ No newline at end of file diff --git a/custom_components/warzone/config_flow.py b/custom_components/warzone/config_flow.py index 8739d8e..17e6bba 100644 --- a/custom_components/warzone/config_flow.py +++ b/custom_components/warzone/config_flow.py @@ -18,7 +18,6 @@ _LOGGER = logging.getLogger(__name__) -# STEP_USER_DATA_SCHEMA = vol.Schema({CONF_USER: str, CONF_PASSWORD: str, vol.Required(CONF_PLATFORM): vol.In([CONF_PLATFORM_XBOX, CONF_PLATFORM_PLAYSTATION]), CONF_PROFILE: str}) STEP_USER_DATA_SCHEMA = vol.Schema({ vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str, @@ -101,9 +100,7 @@ def __init__(self, config_entry): async def async_step_init(self, user_input=None): """Handle the initial step.""" if user_input is not None: - options = {**self._config_entry.options} - options.update(user_input) - return self.async_create_entry(title="", data=options) + return self.async_create_entry(title="", data=user_input) polling = self._config_entry.options.get(CONF_POLLING_INTERVAL, POLLING_INTERVAL) diff --git a/custom_components/warzone/sensor.py b/custom_components/warzone/sensor.py index cc3f6b7..83281f4 100644 --- a/custom_components/warzone/sensor.py +++ b/custom_components/warzone/sensor.py @@ -38,6 +38,7 @@ async def async_update_data(): finalResults["level"] = profileResults["level"] return finalResults except Exception as exception: + _LOGGER.error("Failed to update warzone", exc_info=1) raise UpdateFailed(exception) polling = entry.options.get(CONF_POLLING_INTERVAL, POLLING_INTERVAL)