Skip to content

Commit

Permalink
Better error handling getting kumo_config
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dlarrick committed Jan 12, 2020
1 parent 698af63 commit bde56d5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
60 changes: 41 additions & 19 deletions custom_components/kumo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
11 changes: 10 additions & 1 deletion custom_components/kumo/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/kumo/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit bde56d5

Please sign in to comment.