Skip to content

Commit

Permalink
V2.0.3
Browse files Browse the repository at this point in the history
Handle API call fails more gracefully
API count across multiple instances of the integration
  • Loading branch information
petergridge authored May 17, 2023
1 parent a75c37d commit 4d0ca07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion custom_components/openweathermaphistory/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from homeassistant.helpers.httpx_client import get_async_client
from homeassistant.core import HomeAssistant

DEFAULT_TIMEOUT = 15
DEFAULT_TIMEOUT = 5

_LOGGER = logging.getLogger(__name__)

Expand Down
30 changes: 17 additions & 13 deletions custom_components/openweathermaphistory/weatherhistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = "Open Weather Map History"
DEFAULT_NAME = "OpenWeatherMap History"

class Weather():
"""weather class"""
Expand All @@ -53,8 +53,7 @@ def __init__(
self._maxcalls = config.get(CONF_MAX_CALLS,1000)
self._backlog = 0
self._processing_type = None
self._daily_count = 1

self._daily_count = 1

def get_stored_data(self, name):
"""Return stored data."""
Expand Down Expand Up @@ -97,17 +96,17 @@ async def get_forecastdata(self):
url = CONST_API_FORECAST % (self._lat,self._lon, self._key)
rest = RestData()
await rest.set_resource(self._hass, url)
await rest.async_update(log_errors=True)
self._daily_count += 1
await rest.async_update(log_errors=False)

try:
data = json.loads(rest.data)
#check if the call was successful
days = data.get('daily',{})
current = data.get('current',{})
except TypeError:
_LOGGER.error('OpenWeatherMap forecast call failed')
_LOGGER.warning('OpenWeatherMap forecast call failed')
return
self._daily_count += 1

#current observations
currentdata = {"rain":current.get('rain',{}).get('1h',0)
Expand Down Expand Up @@ -230,7 +229,6 @@ async def async_update(self):
self._daily_count = 0
dailycalls = {'time': midnight,'count':self._daily_count}
self.store_data({ 'dailycalls':dailycalls},'owm_api_count')
_LOGGER.warning('daily count reset')
warning_issued = False

#do not process when no calls remaining
Expand Down Expand Up @@ -301,6 +299,9 @@ async def get_historydata(self,historydata):
#increment last date by an hour
lastdt += 3600
hourdata = await self.gethourdata(lastdt)
if hourdata == {}:
return historydata

data.update({lastdt : hourdata })
#end rest loop
return data
Expand All @@ -322,6 +323,8 @@ async def async_backload(self,historydata):
self._backlog -= 1
startdp -= 3600
hourdata = await self.gethourdata(startdp)
if hourdata == {}:
return
data.update({startdp : hourdata })
return data

Expand All @@ -331,16 +334,17 @@ async def gethourdata(self,timestamp):
rest = RestData()
await rest.set_resource(self._hass, url)
await rest.async_update(log_errors=False)
self._daily_count += 1
data = json.loads(rest.data)
#check if the call was successful
self.validate_data(data)

try:
data = json.loads(rest.data)
current = data.get('data')[0]
if current is None:
current = {}
except ValueError:
_LOGGER.error('OpenWeatherMap history call failed')
except TypeError:
_LOGGER.warning('OpenWeatherMap history call failed')
return {}
self._daily_count += 1

#build this hours data
precipval = {}
preciptypes = ['rain','snow']
Expand Down

0 comments on commit 4d0ca07

Please sign in to comment.