Skip to content

Commit

Permalink
Levoit Air Purifier Timer Debug and Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
webdjoe committed May 19, 2023
1 parent 7c7b73d commit 4aec456
Show file tree
Hide file tree
Showing 17 changed files with 398 additions and 139 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pyvesync is a library to manage VeSync compatible [smart home devices](#supporte
- [Cosori 3.7 and 5.8 Quart Air Fryer](#cosori-37-and-58-quart-air-fryer)
- [Air Fryer Properties](#air-fryer-properties)
- [Air Fryer Methods](#air-fryer-methods)
- [Timer Dataclass](#timer-dataclass)
- [Timer DataClass](#timer-dataclass)
- [JSON Output API](#json-output-api)
- [JSON Output for All Devices](#json-output-for-all-devices)
- [JSON Output for Outlets](#json-output-for-outlets)
Expand Down Expand Up @@ -343,9 +343,9 @@ Compatible levels for each model:

`VeSyncFan.set_night_light('on'|'dim'|'off')` - Set night light brightness

`VeSyncFan.get_timer()` - Get any running timers, returns Timer Dataclass
`VeSyncFan.get_timer()` - Get any running timers, stores Timer DataClass in `VeSyncFan.timer`

`VeSyncFan.set_timer(timer_duration=3000, action='off')` - Set a timer for the device, duration in seconds, action is either 'on', 'off' or 'sleep'
`VeSyncFan.set_timer(timer_duration=3000)` - Set a timer for the device, only turns device off. Timer DataClass stored in `VeSyncFan.timer`

`VeSyncFan.clear_timer()` - Cancel any running timer

Expand Down Expand Up @@ -600,16 +600,17 @@ They can be set through the `VeSyncAirFryer158.fryer_status` dataclass but shoul
`VeSyncAirFryer158.end()` - End cooking or preheating and return air fryer to `standby` state


### Timer Dataclass
### Timer DataClass

Currently *only implemented for Levoit Core 200S and 300S Air Purifier*, will eventually integrate with remaining devices. This object is created when `get_timer()` or `set_timer()` is called.
This is the a Timer DataClass that is used in the `get_timer()` or `set_timer()` methods *only implemented for Levoit Core 200S and 300S Air Purifier*, will eventually integrate with remaining devices. This object is created when the device timer methods are called. **The `pause()`, `resume()` and `stop()` methods for this DataClass only impact the timer locally and do not update the API.**

```python
from pyvesync.helpers import Timer

timer = Timer(timer_duration=60, action='sleep', id=1)
timer = Timer(timer_duration=60, id=1)

# Get time remaining in seconds
# Calculates based on timer elapsed each time property is called
timer.remaining_time

# Get status
Expand All @@ -618,19 +619,19 @@ timer.status
# Get action
timer.action

# Set status - active, paused, or done
timer.status = 'paused'
# Set status - active or done
timer.status = 'active'

# set time remaining in seconds, does not edit status
timer.remaining_time = 120

# Pause timer
# Pause timer - Does not update API - only pauses locally
timer.pause()

# End timer
# End timer -Does not update API - only ends locally
timer.end()

# Resume timer
# Resume timer - Does not update API - only Resumes locally
timer.start()
```

Expand Down
2 changes: 1 addition & 1 deletion src/pyvesync/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def update(self, *, time_remaining: Optional[int] = None,
self.status = status

def pause(self) -> None:
"""Pause timer."""
"""Pause timer. NOTE - this does not stop the timer via API only locally."""
self._internal_update()
if self.status == 'done':
return
Expand Down
17 changes: 7 additions & 10 deletions src/pyvesync/vesyncfan.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ def update(self):
def get_timer(self) -> Optional[Timer]:
"""Retrieve running timer from purifier."""
head, body = self.build_api_dict('getTimer')
body['payload']['data'] = {}
if not head and not body:
return None

Expand Down Expand Up @@ -297,27 +298,23 @@ def get_timer(self) -> Optional[Timer]:
logger.debug('Timer found: %s', str(self.timer))
return self.timer

def set_timer(self, timer_duration: int, action: str) -> bool:
def set_timer(self, timer_duration: int) -> bool:
"""Set timer for Purifier.
Arguments
----------
timer_duration: int
Duration of timer in seconds
action: str
Action to take when timer expires, can be off, on, or sleep
"""
if action not in ['off', 'on', 'sleep']:
logger.debug('Invalid action %s', action)
return False

if self.device_status != 'on':
logger.debug("Can't set timer when device is off")
head, body = self.build_api_dict('addTimer')
if not head and not body:
return False

body['payload']['data'] = {
'total': timer_duration,
'action': action,
'action': 'off',
}

r, _ = Helpers.call_api(
Expand All @@ -337,11 +334,11 @@ def set_timer(self, timer_duration: int, action: str) -> bool:
timer_id = r.get('result', {}).get('result', {}).get('id')
if timer_id is not None:
self.timer = Timer(timer_duration=timer_duration,
action=action,
action='off',
id=timer_id)
else:
self.timer = Timer(timer_duration=timer_duration,
action=action)
action='off')
return True

def clear_timer(self) -> bool:
Expand Down
50 changes: 50 additions & 0 deletions src/tests/api/vesyncfan/Core200S.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ change_fan_speed:
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
clear_timer:
headers:
Content-Type: application/json; charset=UTF-8
User-Agent: okhttp/3.12.1
json_object:
acceptLanguage: en
accountID: sample_id
appVersion: 2.8.6
cid: Core200S-CID
configModule: ConfigModule
debugMode: false
deviceRegion: US
method: bypassV2
payload:
data: {}
method: getTimer
source: APP
phoneBrand: SM N9005
phoneOS: Android
timeZone: America/New_York
token: sample_tk
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
manual_mode:
headers:
Content-Type: application/json; charset=UTF-8
Expand Down Expand Up @@ -53,6 +77,32 @@ manual_mode:
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
set_timer:
headers:
Content-Type: application/json; charset=UTF-8
User-Agent: okhttp/3.12.1
json_object:
acceptLanguage: en
accountID: sample_id
appVersion: 2.8.6
cid: Core200S-CID
configModule: ConfigModule
debugMode: false
deviceRegion: US
method: bypassV2
payload:
data:
action: 'off'
total: 100
method: addTimer
source: APP
phoneBrand: SM N9005
phoneOS: Android
timeZone: America/New_York
token: sample_tk
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
sleep_mode:
headers:
Content-Type: application/json; charset=UTF-8
Expand Down
50 changes: 50 additions & 0 deletions src/tests/api/vesyncfan/Core300S.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ change_fan_speed:
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
clear_timer:
headers:
Content-Type: application/json; charset=UTF-8
User-Agent: okhttp/3.12.1
json_object:
acceptLanguage: en
accountID: sample_id
appVersion: 2.8.6
cid: Core300S-CID
configModule: ConfigModule
debugMode: false
deviceRegion: US
method: bypassV2
payload:
data: {}
method: getTimer
source: APP
phoneBrand: SM N9005
phoneOS: Android
timeZone: America/New_York
token: sample_tk
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
manual_mode:
headers:
Content-Type: application/json; charset=UTF-8
Expand Down Expand Up @@ -78,6 +102,32 @@ manual_mode:
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
set_timer:
headers:
Content-Type: application/json; charset=UTF-8
User-Agent: okhttp/3.12.1
json_object:
acceptLanguage: en
accountID: sample_id
appVersion: 2.8.6
cid: Core300S-CID
configModule: ConfigModule
debugMode: false
deviceRegion: US
method: bypassV2
payload:
data:
action: 'off'
total: 100
method: addTimer
source: APP
phoneBrand: SM N9005
phoneOS: Android
timeZone: America/New_York
token: sample_tk
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
sleep_mode:
headers:
Content-Type: application/json; charset=UTF-8
Expand Down
50 changes: 50 additions & 0 deletions src/tests/api/vesyncfan/Core400S.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ change_fan_speed:
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
clear_timer:
headers:
Content-Type: application/json; charset=UTF-8
User-Agent: okhttp/3.12.1
json_object:
acceptLanguage: en
accountID: sample_id
appVersion: 2.8.6
cid: Core400S-CID
configModule: ConfigModule
debugMode: false
deviceRegion: US
method: bypassV2
payload:
data: {}
method: getTimer
source: APP
phoneBrand: SM N9005
phoneOS: Android
timeZone: America/New_York
token: sample_tk
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
manual_mode:
headers:
Content-Type: application/json; charset=UTF-8
Expand Down Expand Up @@ -78,6 +102,32 @@ manual_mode:
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
set_timer:
headers:
Content-Type: application/json; charset=UTF-8
User-Agent: okhttp/3.12.1
json_object:
acceptLanguage: en
accountID: sample_id
appVersion: 2.8.6
cid: Core400S-CID
configModule: ConfigModule
debugMode: false
deviceRegion: US
method: bypassV2
payload:
data:
action: 'off'
total: 100
method: addTimer
source: APP
phoneBrand: SM N9005
phoneOS: Android
timeZone: America/New_York
token: sample_tk
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
sleep_mode:
headers:
Content-Type: application/json; charset=UTF-8
Expand Down
50 changes: 50 additions & 0 deletions src/tests/api/vesyncfan/Core600S.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ change_fan_speed:
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
clear_timer:
headers:
Content-Type: application/json; charset=UTF-8
User-Agent: okhttp/3.12.1
json_object:
acceptLanguage: en
accountID: sample_id
appVersion: 2.8.6
cid: Core600S-CID
configModule: ConfigModule
debugMode: false
deviceRegion: US
method: bypassV2
payload:
data: {}
method: getTimer
source: APP
phoneBrand: SM N9005
phoneOS: Android
timeZone: America/New_York
token: sample_tk
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
manual_mode:
headers:
Content-Type: application/json; charset=UTF-8
Expand Down Expand Up @@ -78,6 +102,32 @@ manual_mode:
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
set_timer:
headers:
Content-Type: application/json; charset=UTF-8
User-Agent: okhttp/3.12.1
json_object:
acceptLanguage: en
accountID: sample_id
appVersion: 2.8.6
cid: Core600S-CID
configModule: ConfigModule
debugMode: false
deviceRegion: US
method: bypassV2
payload:
data:
action: 'off'
total: 100
method: addTimer
source: APP
phoneBrand: SM N9005
phoneOS: Android
timeZone: America/New_York
token: sample_tk
traceId: TRACE_ID
method: post
url: /cloud/v2/deviceManaged/bypassV2
sleep_mode:
headers:
Content-Type: application/json; charset=UTF-8
Expand Down
Loading

0 comments on commit 4aec456

Please sign in to comment.