Skip to content

Commit

Permalink
Fix fetching state at startup so entities are ready for use sooner
Browse files Browse the repository at this point in the history
Fix one more async violation in fetching kumo_cache.json
Bump pykumo to 0.1.7, fixes flakiness in reporting just-set states
  • Loading branch information
dlarrick committed May 24, 2020
1 parent 6bd0b68 commit 73f2f80
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
8 changes: 5 additions & 3 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.6"]
REQUIREMENTS = ["pykumo==0.1.7"]
DOMAIN = "kumo"
KUMO_DATA = "kumo_data"
KUMO_CONFIG_CACHE = "kumo_cache.json"
Expand Down Expand Up @@ -83,7 +83,8 @@ async def async_setup(hass, config):
else:
# Try to load from server
account = pykumo.KumoCloudAccount(username, password)
if account.try_setup():
setup_success = await hass.async_add_executor_job(account.try_setup)
if setup_success:
if prefer_cache:
_LOGGER.info("Loaded config from local cache")
success = True
Expand All @@ -104,7 +105,8 @@ async def async_setup(hass, config):
load_json, hass.config.path(KUMO_CONFIG_CACHE)
) or {"fetched": False}
account = pykumo.KumoCloudAccount(username, password, kumo_dict=cached_json)
if account.try_setup():
setup_success = await hass.async_add_executor_job(account.try_setup)
if setup_success:
if prefer_cache:
await hass.async_add_executor_job(
save_json,
Expand Down
16 changes: 10 additions & 6 deletions custom_components/kumo/climate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""HomeAssistant climate component for KumoCloud connected HVAC units."""
import logging
import pprint
from datetime import timedelta

import voluptuous as vol

Expand Down Expand Up @@ -103,7 +104,9 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
kumo_api = pykumo.PyKumo(name, address, credentials,
(connect_timeout, response_timeout))
await hass.async_add_executor_job(kumo_api.update_status)
devices.append(KumoThermostat(kumo_api))
kumo_thermostat = KumoThermostat(kumo_api)
await hass.async_add_executor_job(kumo_thermostat.update)
devices.append(kumo_thermostat)
_LOGGER.debug("Kumo adding entity: %s", name)
async_add_entities(devices)

Expand Down Expand Up @@ -170,13 +173,14 @@ def __init__(self, kumo_api):
prop,
str(err),
)
self._available = False

self._available = True

def update(self):
"""Call from HA to trigger a refresh of cached state."""
for prop in KumoThermostat._update_properties:
self._update_property(prop)
self._available = True

def _update_property(self, prop):
"""Call to refresh the value of a property -- may block on I/O."""
Expand Down Expand Up @@ -426,7 +430,7 @@ def set_temperature(self, **kwargs):
response = self._pykumo.set_heat_setpoint(target["heat"])
response += " " + self._pykumo.set_cool_setpoint(["cool"])
_LOGGER.debug("Kumo %s set temp: %s C", self._name, target)
_LOGGER.info("Kumo %s set temp response: %s", self._name, response)
_LOGGER.debug("Kumo %s set temp response: %s", self._name, response)

def set_hvac_mode(self, hvac_mode):
"""Set new target operation mode."""
Expand All @@ -436,14 +440,14 @@ def set_hvac_mode(self, hvac_mode):
mode = "off"

response = self._pykumo.set_mode(mode)
_LOGGER.info("Kumo %s set mode response: %s", self._name, response)
_LOGGER.debug("Kumo %s set mode %s response: %s", self._name, hvac_mode, response)

def set_swing_mode(self, swing_mode):
"""Set new vane swing mode."""
response = self._pykumo.set_vane_direction(swing_mode)
_LOGGER.info("Kumo %s set swing mode response: %s", self._name, response)
_LOGGER.debug("Kumo %s set swing mode response: %s", self._name, response)

def set_fan_mode(self, fan_mode):
"""Set new fan speed mode."""
response = self._pykumo.set_fan_speed(fan_mode)
_LOGGER.info("Kumo %s set fan speed response: %s", self._name, response)
_LOGGER.debug("Kumo %s set fan speed response: %s", self._name, response)
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.6"],
"requirements": ["pykumo==0.1.7"],
"homeassistant": "0.96.0"
}

0 comments on commit 73f2f80

Please sign in to comment.