From bde56d5bc6e0fc4afe638b38bb028ae083884cf2 Mon Sep 17 00:00:00 2001 From: Doug Larrick Date: Sun, 12 Jan 2020 11:54:01 -0500 Subject: [PATCH] Better error handling getting kumo_config Do kumo_config loading logic in less clever but clearer way Quicker timeouts talking to indoor units Report sensor battery level (if present) as an attribute --- custom_components/kumo/__init__.py | 60 +++++++++++++++++++--------- custom_components/kumo/climate.py | 11 ++++- custom_components/kumo/manifest.json | 2 +- 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/custom_components/kumo/__init__.py b/custom_components/kumo/__init__.py index a2e5b6c..3acf6bb 100644 --- a/custom_components/kumo/__init__.py +++ b/custom_components/kumo/__init__.py @@ -10,7 +10,7 @@ _LOGGER = logging.getLogger(__name__) -REQUIREMENTS = ['pykumo==0.1.3'] +REQUIREMENTS = ['pykumo==0.1.4'] DOMAIN = "kumo" KUMO_DATA = "kumo_data" KUMO_CONFIG_CACHE = "kumo_cache.json" @@ -62,28 +62,50 @@ async def async_setup(hass, config): # Read config from either remote KumoCloud server or # cached JSON. cached_json = {} + success = False if prefer_cache: + # Try to load from cache cached_json = await hass.async_add_executor_job( - load_json, hass.config.path(KUMO_CONFIG_CACHE)) - account = pykumo.KumoCloudAccount(username, password) - if not cached_json and account.try_setup(): - await hass.async_add_executor_job( - save_json, hass.config.path(KUMO_CONFIG_CACHE), - account.get_raw_json()) - _LOGGER.info("Loaded config from KumoCloud server") + load_json, hass.config.path(KUMO_CONFIG_CACHE)) or {"fetched": False} + account = pykumo.KumoCloudAccount(username, password, kumo_dict=cached_json) else: - if not prefer_cache: - cached_json = await hass.async_add_executor_job( - load_json, hass.config.path(KUMO_CONFIG_CACHE)) - if cached_json: - account = pykumo.KumoCloudAccount( - username, password, kumo_dict=cached_json) + # Try to load from server + account = pykumo.KumoCloudAccount(username, password) + if account.try_setup(): + if prefer_cache: _LOGGER.info("Loaded config from local cache") + success = True else: - _LOGGER.warning("Could not load KumoCloud cache") - - hass.data[KUMO_DATA] = KumoData(account) + await hass.async_add_executor_job( + save_json, hass.config.path(KUMO_CONFIG_CACHE), + account.get_raw_json()) + _LOGGER.info("Loaded config from KumoCloud server") + success = True + else: + # Fall back + if prefer_cache: + # Try to load from server + account = pykumo.KumoCloudAccount(username, password) + else: + # Try to load from cache + cached_json = await hass.async_add_executor_job( + load_json, hass.config.path(KUMO_CONFIG_CACHE)) or {"fetched": False} + account = pykumo.KumoCloudAccount(username, password, kumo_dict=cached_json) + if account.try_setup(): + if prefer_cache: + await hass.async_add_executor_job( + save_json, hass.config.path(KUMO_CONFIG_CACHE), + account.get_raw_json()) + _LOGGER.info("Loaded config from KumoCloud server as fallback") + success = True + else: + _LOGGER.info("Loaded config from local cache as fallback") + success = True - setup_kumo(hass, config) + if success: + hass.data[KUMO_DATA] = KumoData(account) + setup_kumo(hass, config) + return True - return True + _LOGGER.warning("Could not load config from KumoCloud server or cache") + return False diff --git a/custom_components/kumo/climate.py b/custom_components/kumo/climate.py index c071eb5..07a936b 100644 --- a/custom_components/kumo/climate.py +++ b/custom_components/kumo/climate.py @@ -13,7 +13,7 @@ HVAC_MODE_DRY, HVAC_MODE_FAN_ONLY, ATTR_HVAC_MODE, CURRENT_HVAC_IDLE, CURRENT_HVAC_COOL, CURRENT_HVAC_HEAT, CURRENT_HVAC_DRY, CURRENT_HVAC_OFF, SUPPORT_TARGET_TEMPERATURE_RANGE) -from homeassistant.const import (TEMP_CELSIUS) +from homeassistant.const import (TEMP_CELSIUS, ATTR_BATTERY_LEVEL) from . import KUMO_DATA @@ -306,6 +306,15 @@ def _update_battery_percent(self): percent = self._pykumo.get_sensor_battery() self._battery_percent = percent + @property + def device_state_attributes(self): + """Return the state attributes of the device.""" + attr = {} + if self._battery_percent is not None: + attr[ATTR_BATTERY_LEVEL] = self._battery_percent + + return attr + @property def should_poll(self): """Return the polling state.""" diff --git a/custom_components/kumo/manifest.json b/custom_components/kumo/manifest.json index cc6d8c1..2cf8bbc 100644 --- a/custom_components/kumo/manifest.json +++ b/custom_components/kumo/manifest.json @@ -4,6 +4,6 @@ "documentation": "https://github.com/dlarrick/hass-kumo", "dependencies": [], "codeowners": [ "@dlarrick" ], - "requirements": ["pykumo==0.1.3"], + "requirements": ["pykumo==0.1.4"], "homeassistant": "0.96.0" }