Skip to content

Commit

Permalink
Add better error handling, extend query window
Browse files Browse the repository at this point in the history
  • Loading branch information
hokiebrian committed Jun 20, 2023
1 parent 9c3606c commit c48e8f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion custom_components/eia_hourly_demand/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/hokiebrian/eia_hourly_demand/issues",
"requirements": [],
"version": "1.0.9"
"version": "1.0.10"
}
13 changes: 10 additions & 3 deletions custom_components/eia_hourly_demand/sensor.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
""" Setup EIA Sensor """
import logging
from datetime import timedelta, date
import json
import aiohttp
from homeassistant.core import HomeAssistant
from homeassistant.components.sensor import SensorEntity, SensorStateClass
from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(seconds=1800)

Expand Down Expand Up @@ -52,16 +54,21 @@ def unique_id(self):
return f"HourlyMWh{self._ba_id}"

async def async_update(self):
start_date = (date.today() - timedelta(days=1)).strftime("%Y-%m-%d")
start_date = (date.today() - timedelta(days=7)).strftime("%Y-%m-%d")
url = EIA_URL.format(
api_key=self._api_key, ba_id=self._ba_id, start_date=start_date
)

_LOGGER.debug(f"Data {url}")
async with aiohttp.ClientSession() as session:
try:
timeout = aiohttp.ClientTimeout(total=5)
async with session.get(url, timeout=timeout) as response:
data = await response.json()
self._state = json.dumps(data["response"]["data"][0]["value"])
except aiohttp.ClientConnectorError:
except aiohttp.ClientConnectorError as e:
_LOGGER.debug(f"Connection Error: {e}")
self._state = None
except (IndexError, KeyError) as e:
_LOGGER.error("Data Error, no data returned")
_LOGGER.debug(f"Data Error: {e}")
self._state = None

0 comments on commit c48e8f3

Please sign in to comment.