Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select Device #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions waterfurnace/waterfurnace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

"""Main module."""
import copy
import logging
import json
import logging
import threading
import time

Expand Down Expand Up @@ -97,9 +97,11 @@ class WFError(WFException):

class WaterFurnace(object):

def __init__(self, user, passwd, max_fails=5):
def __init__(self, user, passwd, max_fails=5, location=0, device=0):
self.user = user
self.passwd = passwd
self.location = location
self.device = device
self.gwid = None
self.sessionid = None
self.tid = 0
Expand Down Expand Up @@ -148,7 +150,49 @@ def _login_ws(self):
recv = self.ws.recv()
data = json.loads(recv)
_LOGGER.debug("Login response: %s" % data)
self.gwid = data["locations"][0]["gateways"][0]["gwid"]

locations = data["locations"]
location = None

if isinstance(self.location, int):
try:
location = locations[self.location]
except Exception:
raise WFError("Location index out of range. Max index is {}".format(len(locations) - 1))
elif isinstance(self.location, str):
for index, location_data in enumerate(locations):
location_description = location_data.get("description")
if location_description == self.location:
location = locations[index]
break

if not location:
raise WFError("Unable to find location: {}".format(self.location))
else:
raise WFError("Unknown location type ({}): {}. Should be int or str".format(type(self.location), self.location))

gateways = location["gateways"]
device = None

if isinstance(self.device, int):
try:
device = gateways[self.device]
except Exception:
raise WFError("Device index out of range. Max index is {}".format(len(gateways) - 1))
elif isinstance(self.device, str):
for index, gateway_data in enumerate(gateways):
gateway_gwid = gateway_data.get("gwid")
gateway_description = gateway_data.get("description")
if gateway_gwid == self.device or gateway_description == self.device:
device = gateways[index]
break

if not device:
raise WFError("Unable to find device: {}".format(self.device))
else:
raise WFError("Unknown device type ({}): {}. Should be int or str".format(type(self.location), self.location))

self.gwid = device["gwid"]
self.next_tid()

def login(self):
Expand Down