Skip to content

Commit

Permalink
switch to axios and add all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rossdargan committed Nov 24, 2020
1 parent ee6818a commit 3573bd7
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 70 deletions.
28 changes: 28 additions & 0 deletions lazy_spa/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json
import aiohttp
import asyncio
from datetime import datetime
from .const import (
API_URI,
API_VERSION
)
from .errors import InvalidPasswordOrEmail, UnexpectedResponse
class Api:
"""The class to handle communicating with the api"""
def __init__(self, auth, base_url=API_URI + "/v" + API_VERSION + "/gizwits/"):
"""
constructor
"""
self.auth = auth
self.base_url = base_url

async def send_command(self, command):
async with aiohttp.ClientSession() as session:
async with session.post(self.base_url+command, data=self.auth) as r:

if r.status == 200:
return await r.json()
if r.status == 403:
raise InvalidPasswordOrEmail((await r.json())["errors"])
r.raise_for_status()
raise UnexpectedResponse(await r.text())
19 changes: 8 additions & 11 deletions lazy_spa/auth.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Authenticates with Lay-Z-Spa"""
import requests
from .errors import InvalidPasswordOrEmail, UnexpectedResponse

from .api import Api
from .const import (
API_URI,
API_VERSION
)
class Auth:
"""The class to handle authenticating with the API"""

Expand All @@ -10,16 +12,11 @@ def __init__(self):
constructor
"""

def get_token(self, email, password):
async def get_token(self, email, password):
"""
Gets an authentication token for the user
:type email: str
:type password: str
"""
r = requests.post("https://mobileapi.lay-z-spa.co.uk/v1/auth/login", data={"email": email, "password":password})
if r.status_code == 200:
return r.json()
if r.status_code == 403:
raise InvalidPasswordOrEmail(r.json()["errors"])

raise UnexpectedResponse(r.json()["errors"])
api = Api({"email": email, "password":password}, base_url=API_URI + "/v" + API_VERSION )
return await api.send_command("/auth/login")
131 changes: 80 additions & 51 deletions lazy_spa/spa.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,104 @@
"""Authenticates with Lay-Z-Spa"""
import requests
import json
import asyncio
from datetime import datetime
from .const import (
API_URI,
API_VERSION
)
from .api import Api
class Spa:
"""The class to handle authenticating with the API"""
def __init__(self, api, did):
"""
constructor
"""
self.api = api
self.did = did
self.api = Api({"did": did, "api_token":api})

def is_online(self):
async def is_online(self):
"""
Indicates if the device is currently online
"""
data = {"did": self.did, "api_token":self.api}
r = requests.post(API_URI + "/v" + API_VERSION + "/gizwits/is_online", data=data)
if r.status_code == 200:
return r.json()["data"] == "true"
return r
result = await self.api.send_command("is_online")
return result["data"] == "true"

def status(self):
async def update_status(self):
"""
Indicates if the device is currently online
"""
data = {"did": self.did, "api_token":self.api}
r = requests.post(API_URI + "/v" + API_VERSION + "/gizwits/status", data=data)
if r.status_code == 200:
data= r.json()["data"]
print(data);
self.updated_at = datetime.fromtimestamp(data["updated_at"])
attr = data["attr"]

self.wave_appm_min = attr["wave_appm_min"]
self.heat_timer_min = attr["heat_timer_min"]
self.earth = attr["earth"]
self.wave_timer_min = attr["wave_timer_min"]
result = await self.api.send_command("status")

data = result["data"]
self.updated_at = datetime.fromtimestamp(data["updated_at"])
attr = data["attr"]

self.wave_appm_min = attr["wave_appm_min"]
self.heat_timer_min = attr["heat_timer_min"]
self.earth = attr["earth"]
self.wave_timer_min = attr["wave_timer_min"]

self.filter_timer_min = attr["filter_timer_min"]
self.heat_appm_min = attr["heat_appm_min"]
self.filter_appm_min = attr["filter_appm_min"]
self.filter_timer_min = attr["filter_timer_min"]
self.heat_appm_min = attr["heat_appm_min"]
self.filter_appm_min = attr["filter_appm_min"]

self.locked = attr["locked"]
self.locked = attr["locked"]

self.power = attr["power"]
self.heat_power = attr["heat_power"]
self.wave_power = attr["wave_power"]
self.filter_power = attr["filter_power"]
self.power = attr["power"] == 1
self.heat_power = attr["heat_power"] == 1
self.wave_power = attr["wave_power"] == 1
self.filter_power = attr["filter_power"] == 1

self.temp_now = attr["temp_now"]
self.temp_set = attr["temp_set"]
self.temp_set_unit ="°C" if attr["temp_set_unit"]=="摄氏" else "°F"
self.heat_temp_reach = attr["heat_temp_reach"] == 1
self.temp_now = attr["temp_now"]
self.temp_set = attr["temp_set"]
self.temp_set_unit ="°C" if attr["temp_set_unit"]=="摄氏" else "°F"
self.heat_temp_reach = attr["heat_temp_reach"] == 1

self.system_err1 = attr["system_err1"]
self.system_err2 = attr["system_err2"]
self.system_err3 = attr["system_err3"]
self.system_err4 = attr["system_err4"]
self.system_err5 = attr["system_err5"]
self.system_err6 = attr["system_err6"]
self.system_err7 = attr["system_err7"]
self.system_err8 = attr["system_err8"]
self.system_err9 = attr["system_err9"]

async def set_power(self, power):
"""
Turn the spa on or off
"""
if power:
await self.api.send_command("turn_on")
else:
await self.api.send_command("turn_off")
await self.update_status()


async def set_filter_power(self, power):
"""
Turn the filter on or off
"""
if power:
await self.api.send_command("turn_filter_on")
else:
await self.api.send_command("turn_filter_off")
await self.update_status()

self.system_err1 = attr["system_err1"]
self.system_err2 = attr["system_err2"]
self.system_err3 = attr["system_err3"]
self.system_err4 = attr["system_err4"]
self.system_err5 = attr["system_err5"]
self.system_err6 = attr["system_err6"]
self.system_err7 = attr["system_err7"]
self.system_err8 = attr["system_err8"]
self.system_err9 = attr["system_err9"]



return r
async def set_heat_power(self, power):
"""
Turn the heater on or off
"""
if power:
# The filter MUST be turned on if the heater is to be turned on
if(not self.heat_power):
await self.api.send_command("turn_filter_on")
await self.api.send_command("turn_heat_on")
else:
await self.api.send_command("turn_heat_off")
await self.update_status()

async def set_wave_power(self, power):
"""
Turn the bubbles on and off
"""
if power:
await self.api.send_command("turn_wave_on")
else:
await self.api.send_command("turn_wave_off")
await self.update_status()
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pytz
requests>=2.0.0
aiohttp>=3.7.1
23 changes: 16 additions & 7 deletions sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import getpass
from pprint import pprint
from pathlib import Path
import asyncio
from lazy_spa.auth import Auth
from lazy_spa.spa import Spa
from lazy_spa.errors import Error

def main():
async def main():
cache_file = Path("test_token.cache")

if cache_file.is_file():
Expand All @@ -16,17 +16,26 @@ def main():
email = input("Email Address: ")
password = getpass.getpass("Password: ")
auth = Auth()
response = auth.get_token(email, password)
response = await auth.get_token(email, password)
cache_file.write_text(json.dumps(response))
except Error as authError:
print ("Unable to authenticate: ", authError )
return

spa = Spa(response["data"]["api_token"], response["devices"][0]["did"])
res2 = spa.status();
print("current Temp", spa.temp_now);
print ("Authentication Result: ", response["message"])

print(await spa.is_online())
await spa.update_status()
print("current power", spa.power)

await spa.set_power(True)
print("current power 1", spa.power)



# await spa.set_power(True)
print("current power 2", spa.power)

if __name__ == "__main__":
main()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

0 comments on commit 3573bd7

Please sign in to comment.