Skip to content

Commit

Permalink
fix: better error handling, support firmware 215
Browse files Browse the repository at this point in the history
  • Loading branch information
vinteo committed Jun 2, 2020
1 parent 27e7801 commit 00b81ff
Showing 1 changed file with 48 additions and 16 deletions.
64 changes: 48 additions & 16 deletions pyopensprinkler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
from pyopensprinkler.station import Station


class OpenSprinklerAuthError(Exception):
"""Exception for authentication error."""


class OpenSprinklerConnectionError(Exception):
"""Exception for connection error."""


class OpenSprinklerNoStateError(Exception):
"""Exception for no state."""


class OpenSprinklerApiError(Exception):
"""Exception for an error returned by the API."""


class Controller(object):
"""OpenSprinkler Controller"""

Expand Down Expand Up @@ -81,14 +97,22 @@ def request(self, path, params=None, raw_qs=None, refresh_on_update=None):

return resp, content

@on_exception(expo, Exception, max_tries=3)
@on_exception(expo, OpenSprinklerConnectionError, max_tries=3)
def request_http(self, url):
try:
(resp, content) = self._http_client.request(url, "GET")
content = json.loads(content.decode("UTF-8"))

if len(content) == 1 and not content["result"] and content["fwv"]:
raise OpenSprinklerAuthError("Invalid password")
if len(content) == 1:
if "result" in content:
if content["result"] == 2:
raise OpenSprinklerAuthError("Invalid password")
elif content["result"] > 2:
raise OpenSprinklerApiError(
f"Error code: {content['result']}", content["result"]
)
elif "fwv" in content:
raise OpenSprinklerAuthError("Invalid password")

return resp, content
except httplib2.HttpLib2Error as exc:
Expand All @@ -113,7 +137,27 @@ def refresh(self):
self._stations[i] = Station(self, i)

def _refresh_state(self):
(_, content) = self.request("/ja")
try:
(_, content) = self.request("/ja")
except OpenSprinklerApiError as exc:
(_, err_code) = exc.args
if err_code == 32:
# Backwards compatibility for pre 2.1.6
(_, settings) = self.request("/jc")
(_, options) = self.request("/jo")
(_, stations) = self.request("/jn")
(_, status) = self.request("/js")
(_, programs) = self.request("/jp")
content = {
"settings": settings,
"options": options,
"stations": stations,
"status": status,
"programs": programs,
}
else:
raise exc

self._state = content

def _retrieve_state(self):
Expand Down Expand Up @@ -730,15 +774,3 @@ def programs(self):
def stations(self):
"""Return stations"""
return self._stations


class OpenSprinklerAuthError(Exception):
"""Exception for authentication error."""


class OpenSprinklerConnectionError(Exception):
"""Exception for connection error."""


class OpenSprinklerNoStateError(Exception):
"""Exception for no state."""

0 comments on commit 00b81ff

Please sign in to comment.