Skip to content

Commit

Permalink
Merge pull request #27 from petretiandrea/fix/light-state-gui
Browse files Browse the repository at this point in the history
fix issue #23
  • Loading branch information
petretiandrea authored Apr 26, 2021
2 parents ce51e90 + 5e1e43b commit dd9e056
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions custom_components/tapo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""The tapo integration."""
import logging
import asyncio
import async_timeout
from datetime import timedelta

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import UpdateFailed
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.debounce import Debouncer

from .const import DOMAIN, PLATFORMS, CONF_HOST, CONF_USERNAME, CONF_PASSWORD

Expand Down Expand Up @@ -63,19 +66,23 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):

return unload_ok

SCAN_INTERVAL = timedelta(seconds=30)
DEBOUNCER_COOLDOWN = 2

class TapoUpdateCoordinator(DataUpdateCoordinator[TapoDeviceState]):
def __init__(self, hass: HomeAssistant, client: TapoApiClient):
self.api = client
super().__init__(hass, _LOGGGER, name=DOMAIN)
debouncer = Debouncer(hass, _LOGGGER, cooldown=DEBOUNCER_COOLDOWN, immediate=True)
super().__init__(hass, _LOGGGER, name=DOMAIN, update_interval=SCAN_INTERVAL, request_refresh_debouncer=debouncer)

@property
def tapo_client(self) -> TapoApiClient:
return self.api

async def _async_update_data(self):
try:
return await self._update_with_fallback()
async with async_timeout.timeout(10):
return await self._update_with_fallback()
except Exception as exception:
raise UpdateFailed() from exception

Expand Down
4 changes: 2 additions & 2 deletions custom_components/tapo/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def is_on(self):

async def async_turn_on(self):
await self._execute_with_fallback(self._tapo_coordinator.api.on)
await self._tapo_coordinator.async_refresh()
await self._tapo_coordinator.async_request_refresh()

async def async_turn_off(self):
await self._execute_with_fallback(self._tapo_coordinator.api.off)
await self._tapo_coordinator.async_refresh()
await self._tapo_coordinator.async_request_refresh()

0 comments on commit dd9e056

Please sign in to comment.