Skip to content

Commit

Permalink
Fix for lock error
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Mulliken committed May 18, 2021
1 parent 6a5be46 commit 2e6c48f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 11 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[metadata]
# replace with your username:
name = wyzeapy
version = 0.0.23-beta.8
version = 0.0.23-beta.9
author = Mulliken LLC
author_email = [email protected]
description = Python client for private Wyze API
Expand Down
22 changes: 13 additions & 9 deletions src/wyzeapy/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import datetime
import hashlib
import json
import logging
import time
from typing import Any

Expand All @@ -19,6 +20,7 @@
from .exceptions import *
from .types import ResponseCodes, Device, DeviceTypes, ThermostatProps, Group

_LOGGER = logging.getLogger(__name__)

class BaseClient:
access_token = ""
Expand All @@ -27,6 +29,9 @@ class BaseClient:
def __init__(self):
self._session: CacheControl = cachecontrol.CacheControl(requests.Session())

def __del__(self):
self._session.close()

def login(self, email, password) -> bool:
email = email
password = password
Expand All @@ -40,17 +45,18 @@ def login(self, email, password) -> bool:
"X-API-Key": API_KEY
}

response_json = self._session.post("https://auth-prod.api.wyze.com/user/login",
response_json: dict = self._session.post("https://auth-prod.api.wyze.com/user/login",
headers=headers, json=login_payload).json()

try:
self.access_token = response_json['access_token']
self.refresh_token = response_json['refresh_token']
return True
except KeyError as e:
print(e)
if response_json.get('errorCode') is not None:
_LOGGER.error(f"Unable to login with response from Wyze: {response_json}")
return False

self.access_token = response_json['access_token']
self.refresh_token = response_json['refresh_token']

return True

def can_login(self, username, password):
return self.login(username, password)

Expand Down Expand Up @@ -353,8 +359,6 @@ def lock_control(self, device: Device, action: str):

response_json = self._session.post(url, json=payload).json()

self.check_for_errors_lock(response_json)

return response_json

def thermostat_get_iot_prop(self, device: Device):
Expand Down
6 changes: 5 additions & 1 deletion src/wyzeapy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ def __init__(self, email, password):
self.password = password

self.client = BaseClient()
self.client.login(self.email, self.password)
self._valid_login = self.client.login(self.email, self.password)

@property
def valid_login(self):
return self._valid_login

def reauthenticate(self) -> None:
self.client.login(self.email, self.password)
Expand Down
5 changes: 5 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (c) 2021. Mulliken, LLC - All Rights Reserved
# You may use, distribute and modify this code under the terms
# of the attached license. You should have received a copy of
# the license with this file. If not, please write to:
# [email protected] to receive a copy
104 changes: 104 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Copyright (c) 2021. Mulliken, LLC - All Rights Reserved
# You may use, distribute and modify this code under the terms
# of the attached license. You should have received a copy of
# the license with this file. If not, please write to:
# [email protected] to receive a copy

import os
import unittest
from unittest import mock
from typing import List

from src.wyzeapy.client import Client
from src.wyzeapy.types import Device, ThermostatProps


class TestLogin(unittest.TestCase):
def test_can_login(self):
client = Client(os.getenv("WYZE_EMAIL"), os.getenv("WYZE_PASSWORD"))

self.assertTrue(client.valid_login)

def test_bad_login(self):
client = Client("[email protected]", "BadPassword123")

self.assertFalse(client.valid_login)


class TestFunctions(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls._client = Client(os.getenv("WYZE_EMAIL"), os.getenv("WYZE_PASSWORD"))

def test_get_devices_return_type(self):
devices = self._client.get_devices()
self.assertIsInstance(devices, List)
for device in devices:
self.assertIsInstance(device, Device)

def test_turn_on_color_bulb(self):
test_bulb = Device({
'mac': '7C78B214CF40',
'product_type': 'MeshLight',
'product_model': 'WLPA19C'
})

self._client.turn_on(test_bulb)

def test_turn_off_color_bulb(self):
test_bulb = Device({
'mac': '7C78B214CF40',
'product_type': 'MeshLight',
'product_model': 'WLPA19C'
})

self._client.turn_off(test_bulb)

def test_turn_on_bulb(self):
test_bulb = Device({
'mac': '2CAA8E325C52',
'product_type': 'Light',
'product_model': 'WLPA19'
})

self._client.turn_on(test_bulb)

def test_turn_off_bulb(self):
test_bulb = Device({
'mac': '2CAA8E325C52',
'product_type': 'Light',
'product_model': 'WLPA19'
})

self._client.turn_off(test_bulb)

def test_unlock_door(self):
test_lock = Device({
'mac': 'YD.LO1.46c36fcce6c550e527ff79bd8cef59c2',
'product_type': 'Lock',
'product_model': 'WLCK1'
})

self._client.turn_off(test_lock)

def test_lock_door(self):
test_lock = Device({
'mac': 'YD.LO1.46c36fcce6c550e527ff79bd8cef59c2',
'product_type': 'Lock',
'product_model': 'WLCK1'
})

self._client.turn_on(test_lock)

def test_can_read_thermostat(self):
test_thermostat = Device({
'mac': 'CO_EA1_31304635143637394a414e75',
'product_type': 'Thermostat',
'product_model': 'CO_EA1'
})

info = self._client.get_thermostat_info(test_thermostat)

self.assertIsInstance(info, List)
for item in info:
self.assertIsInstance(info, ThermostatProps)

0 comments on commit 2e6c48f

Please sign in to comment.