Skip to content

Commit

Permalink
Fixed #10, decreased default attempts, bump to v1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
swartjean committed May 9, 2021
1 parent c1197d9 commit dd91197
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion custom_components/eskom_loadshedding/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
NAME = "Eskom Loadshedding Interface"
DOMAIN = "eskom_loadshedding"
DOMAIN_DATA = f"{DOMAIN}_data"
VERSION = "1.0.3"
VERSION = "1.0.4"

ISSUE_URL = "https://github.com/swartjean/ha-eskom-loadshedding/issues"

Expand Down
32 changes: 22 additions & 10 deletions custom_components/eskom_loadshedding/eskom_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,41 @@ async def async_query_api(self, endpoint, payload=None):
) as res:
return await res.json()

async def async_get_stage(self, attempts=50):
async def async_get_stage(self, attempts=30):
"""Fetches the current loadshedding stage from the Eskom API
Args:
attempts (int, optional): The number of attempts to query a sane value from the Eskom API. Defaults to 5.
attempts (int, optional): The number of attempts to query a sane value from the Eskom API. Defaults to 30.
Returns:
The loadshedding stage if the query succeeded, else `None`
"""

# Placeholder for returned loadshedding stage
api_result = None

# Query the API until a sensible (> 0) value is received, or the number of attempts is exceeded
for attempt in range(attempts):
res = await self.async_query_api("/GetStatus")

# Return the current loadshedding stage by subtracting 1 from the query result
# Occasionally the Eskom API will return a negative stage, so simply retry if this occurs
if res and int(res) > 0:
return int(res) - 1
# Check if the API returned a valid response
if res:
# Store the response
api_result = res

# Only return the result if the API returned a non-negative stage, otherwise retry
if int(res) > 0:
# Return the current loadshedding stage by subtracting 1 from the query result
return int(res) - 1

# If the query does not succeed after the number of attempts has been exceeded, raise an exception
raise Exception(
f"Error, invalid loadshedding stage received from API after {attempts} attempts"
)
if api_result:
# If the API is up but returning "invalid" stages (< 0), simply return 0
return 0
else:
# If the API the query did not succeed after the number of attempts has been exceeded, raise an exception
raise Exception(
f"Error, no response received from API after {attempts} attempts"
)

async def async_get_data(self):
"""Fetches data from the loadshedding API"""
Expand Down
10 changes: 6 additions & 4 deletions custom_components/eskom_loadshedding/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"domain": "eskom_loadshedding",
"name": "Eskom Loadshedding Interface",
"documentation": "https://github.com/swartjean/ha-eskom-loadshedding",
"issue_tracker": "https://github.com/swartjean/ha-eskom-loadshedding/issues",
"issue_tracker": "https://github.com/swartjean/ha-eskom-loadshedding/issues",
"dependencies": [],
"config_flow": true,
"version": "1.0.3",
"version": "1.0.4",
"codeowners": [
"@swartjean"
],
"requirements": ["aiohttp-retry==1.0"]
}
"requirements": [
"aiohttp-retry==1.0"
]
}

0 comments on commit dd91197

Please sign in to comment.