Skip to content

Commit 9d9144d

Browse files
authored
Merge pull request #4 from fritsvp/master
Updated local API to v2.
2 parents 7944d23 + 9dc33db commit 9d9144d

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

Diff for: goslideapi/goslideapi.py

+25-11
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ async def household_set(self, name, address, lat, lon):
433433
class GoSlideLocal:
434434
"""API Wrapper for the Go Slide devices, local connectivity."""
435435

436-
def __init__(self, timeout=DEFAULT_TIMEOUT, authexception=True, apiversion=1):
436+
def __init__(self, timeout=DEFAULT_TIMEOUT, authexception=True, apiversion=2):
437437
"""Create the object with required parameters."""
438438
self._timeout = timeout
439439
self._authexception = authexception
@@ -589,13 +589,16 @@ async def _request(self, hostname, password, reqtype, uri, data=None):
589589
# format URL with hostname/ip and uri value
590590
url = "http://{}{}".format(hostname, uri)
591591

592-
# First request, should return a 401 error
593-
respstatus, resptext = await self._dorequest(reqtype, url)
592+
# First request, should return a 401 error for v1
593+
# First request is not required for v2
594594

595595
# Default is version 1, when we do WWW-Authentication
596596
if self._apiversion == 1:
597597

598-
# Only a 401 response is correct
598+
#do request to obtain a WWW-authentication header:
599+
respstatus, resptext = await self._dorequest(reqtype, url)
600+
601+
#Only a 401 response is correct
599602
if respstatus == 401:
600603

601604
# The resptext contains the WWW-Authentication header
@@ -616,17 +619,28 @@ async def _request(self, hostname, password, reqtype, uri, data=None):
616619
else:
617620
# We expected a 401 Digest Auth here
618621
_LOGGER.error(
619-
"Failed request with Local API. Received HTTPCode=%s, expected HTTPCode=401",
622+
"Failed request with Local API v1. Received HTTPCode=%s, expected HTTPCode=401. Maybe try switching to v2?",
620623
respstatus,
621624
)
622-
else:
623-
if respstatus == 200:
624-
return resptext
625625

626-
# Anything else is an error
626+
elif self._apiversion == 2:
627+
628+
respstatus, resptext = await self._dorequest(
629+
reqtype, url, data=data
630+
)
631+
632+
if respstatus == 200:
633+
return resptext
634+
635+
# Anything else is an error
636+
_LOGGER.error(
637+
"Failed request Local API v2. HTTPCode=%s",
638+
respstatus,
639+
)
640+
641+
else:
627642
_LOGGER.error(
628-
"Failed request with Local API. HTTPCode=%s",
629-
respstatus,
643+
"Only v1 and v2 is supported.",
630644
)
631645

632646
return None

0 commit comments

Comments
 (0)