-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is the second part of a three-part change to refactor this code from libpurecool to libdyson. This part replaces the discovery components (e.g; DysonDevice.auto_connect in libpurecool) to the separate API provided by libdyson. This also adapts the libpurecool_adapter shim to have a libdyson-like get_device() method to make switching fully over easier in the next change.
- Loading branch information
Showing
6 changed files
with
219 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,47 @@ | ||
"""An adapter to use libpurecool's Dyson support without the Cloud API.""" | ||
|
||
import collections | ||
import logging | ||
from typing import Callable, Dict, List, Optional | ||
from typing import Optional | ||
|
||
from libpurecool import dyson, dyson_device | ||
|
||
|
||
class DysonAccountCache: | ||
def __init__(self, device_cache: List[Dict[str, str]]): | ||
self._devices = self._load(device_cache) | ||
|
||
def _identify(self, device: Dict[str, str]) -> Optional[Callable[[object], object]]: | ||
if dyson.is_360_eye_device(device): | ||
logging.info( | ||
'Identified %s as a Dyson 360 Eye device which is unsupported (ignoring)') | ||
return None | ||
elif dyson.is_heating_device(device): | ||
logging.info( | ||
'Identified %s as a Dyson Pure Hot+Cool Link (V1) device', device['Serial']) | ||
return dyson.DysonPureHotCoolLink | ||
elif dyson.is_dyson_pure_cool_device(device): | ||
logging.info( | ||
'Identified %s as a Dyson Pure Cool (V2) device', device['Serial']) | ||
return dyson.DysonPureCool | ||
elif dyson.is_heating_device_v2(device): | ||
logging.info( | ||
'Identified %s as a Dyson Pure Hot+Cool (V2) device', device['Serial']) | ||
return dyson.DysonPureHotCool | ||
else: | ||
logging.info( | ||
'Identified %s as a Dyson Pure Cool Link (V1) device', device['Serial']) | ||
return dyson.DysonPureCoolLink | ||
|
||
def _load(self, device_cache: List[Dict[str, str]]): | ||
ret = [] | ||
|
||
# Monkey-patch this as we store the local credential unencrypted. | ||
dyson_device.decrypt_password = lambda s: s | ||
|
||
for d in device_cache: | ||
typ = self._identify(d) | ||
if typ: | ||
ret.append(typ(d)) | ||
|
||
return ret | ||
|
||
def devices(self): | ||
return self._devices | ||
# We expect unencrypted credentials only, so monkey-patch this. | ||
dyson_device.decrypt_password = lambda s: s | ||
|
||
|
||
def get_device(name: str, serial: str, credentials: str, product_type: str) -> Optional[object]: | ||
"""Creates a libpurecool DysonDevice based on the input parameters. | ||
Args: | ||
name: name of device (e.g; "Living room") | ||
serial: serial number, e.g; AB1-XX-1234ABCD | ||
credentials: unencrypted credentials for accessing the device locally | ||
product_type: stringified int for the product type (e.g; "455") | ||
""" | ||
device = {'Serial': serial, 'Name': name, | ||
'LocalCredentials': credentials, 'ProductType': product_type, | ||
'Version': '', 'AutoUpdate': '', 'NewVersionAvailable': ''} | ||
|
||
if dyson.is_360_eye_device(device): | ||
logging.info( | ||
'Identified %s as a Dyson 360 Eye device which is unsupported (ignoring)') | ||
return None | ||
|
||
if dyson.is_heating_device(device): | ||
logging.info( | ||
'Identified %s as a Dyson Pure Hot+Cool Link (V1) device', serial) | ||
return dyson.DysonPureHotCoolLink(device) | ||
if dyson.is_dyson_pure_cool_device(device): | ||
logging.info( | ||
'Identified %s as a Dyson Pure Cool (V2) device', serial) | ||
return dyson.DysonPureCool(device) | ||
|
||
if dyson.is_heating_device_v2(device): | ||
logging.info( | ||
'Identified %s as a Dyson Pure Hot+Cool (V2) device',serial) | ||
return dyson.DysonPureHotCool(device) | ||
|
||
# Last chance. | ||
logging.info('Identified %s as a Dyson Pure Cool Link (V1) device', serial) | ||
return dyson.DysonPureCoolLink(device) |
Oops, something went wrong.