Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sensor to only query Glow API shortly after 0 and 30 mins past the hour #136

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions custom_components/hildebrandglow_dcc/sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Platform for sensor integration."""
import logging
from datetime import timedelta
from datetime import datetime, timedelta
from typing import Any, Callable, Dict, Optional
import random
import asyncio

from homeassistant.components.sensor import (
DEVICE_CLASS_ENERGY,
Expand Down Expand Up @@ -196,13 +198,18 @@ def unit_of_measurement(self) -> Optional[str]:

async def _glow_update(self, func: Callable) -> None:
"""Get updated data from Glow"""
try:
self._state = await self.hass.async_add_executor_job(
func, self.resource["resourceId"]
)
except InvalidAuth:
_LOGGER.debug("calling auth failed 2")
await Glow.handle_failed_auth(self.config, self.hass)
sleepdelay = (random.randint(60, 180))
minutes = datetime.now().minute
if (0 <= minutes <= 2) or (30 <= minutes <= 32):
_LOGGER.debug(f"Update time, sleeping {sleepdelay}s before talking to API")
await asyncio.sleep(sleepdelay)
try:
self._state = await self.hass.async_add_executor_job(
func, self.resource["resourceId"]
)
except InvalidAuth:
_LOGGER.debug("calling auth failed 2")
await Glow.handle_failed_auth(self.config, self.hass)

async def async_update(self) -> None:
"""Fetch new state data for the sensor.
Expand Down