mirror of
https://github.com/no2chem/wideq.git
synced 2025-05-16 23:30:10 -07:00
Merge pull request #130 from pifou25/deviceDict
add a mapping for every subclass of Device with DeviceType enum as key
This commit is contained in:
commit
ec0da0d4c2
30
example.py
30
example.py
@ -34,9 +34,9 @@ def ls(client):
|
|||||||
print('{0.id}: {0.name} ({0.type.name} {0.model_id})'.format(device))
|
print('{0.id}: {0.name} ({0.type.name} {0.model_id})'.format(device))
|
||||||
|
|
||||||
|
|
||||||
def mon(client, device_id):
|
def gen_mon(client, device_id):
|
||||||
"""Monitor any device, displaying generic information about its
|
"""Monitor any other device but AC device,
|
||||||
status.
|
displaying generic information about its status.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
device = client.get_device(device_id)
|
device = client.get_device(device_id)
|
||||||
@ -72,18 +72,11 @@ def mon(client, device_id):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def ac_mon(client, device_id):
|
def ac_mon(ac):
|
||||||
"""Monitor an AC/HVAC device, showing higher-level information about
|
"""Monitor an AC/HVAC device, showing higher-level information about
|
||||||
its status such as its temperature and operation mode.
|
its status such as its temperature and operation mode.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
device = client.get_device(device_id)
|
|
||||||
if device.type != wideq.DeviceType.AC:
|
|
||||||
print('This is not an AC device.')
|
|
||||||
return
|
|
||||||
|
|
||||||
ac = wideq.ACDevice(client, device)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ac.monitor_start()
|
ac.monitor_start()
|
||||||
except wideq.core.NotConnectedError:
|
except wideq.core.NotConnectedError:
|
||||||
@ -106,6 +99,8 @@ def ac_mon(client, device_id):
|
|||||||
'on' if state.is_on else 'off'
|
'on' if state.is_on else 'off'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
print('no state. Wait 1 more second.')
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
@ -113,6 +108,18 @@ def ac_mon(client, device_id):
|
|||||||
ac.monitor_stop()
|
ac.monitor_stop()
|
||||||
|
|
||||||
|
|
||||||
|
def mon(client, device_id):
|
||||||
|
"""Monitor any device, displaying generic information about its
|
||||||
|
status.
|
||||||
|
"""
|
||||||
|
|
||||||
|
device_class = client.get_device_obj(device_id)
|
||||||
|
if isinstance(device_class, wideq.ACDevice):
|
||||||
|
ac_mon(device_class)
|
||||||
|
else:
|
||||||
|
gen_mon(client, device_id)
|
||||||
|
|
||||||
|
|
||||||
class UserError(Exception):
|
class UserError(Exception):
|
||||||
"""A user-visible command-line error.
|
"""A user-visible command-line error.
|
||||||
"""
|
"""
|
||||||
@ -185,7 +192,6 @@ def ac_config(client, device_id):
|
|||||||
EXAMPLE_COMMANDS = {
|
EXAMPLE_COMMANDS = {
|
||||||
'ls': ls,
|
'ls': ls,
|
||||||
'mon': mon,
|
'mon': mon,
|
||||||
'ac-mon': ac_mon,
|
|
||||||
'set-temp': set_temp,
|
'set-temp': set_temp,
|
||||||
'set-temp-freezer': set_temp_freezer,
|
'set-temp-freezer': set_temp_freezer,
|
||||||
'turn': turn,
|
'turn': turn,
|
||||||
|
@ -422,3 +422,6 @@ class ACStatus(object):
|
|||||||
def is_on(self):
|
def is_on(self):
|
||||||
op = ACOp(lookup_enum('Operation', self.data, self.ac))
|
op = ACOp(lookup_enum('Operation', self.data, self.ac))
|
||||||
return op != ACOp.OFF
|
return op != ACOp.OFF
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "ACStatus(%r %r)" % (self.ac, self.data)
|
||||||
|
@ -12,6 +12,7 @@ from typing import Any, Dict, Generator, List, Optional
|
|||||||
|
|
||||||
from . import core
|
from . import core
|
||||||
|
|
||||||
|
|
||||||
#: Represents an unknown enum value.
|
#: Represents an unknown enum value.
|
||||||
_UNKNOWN = 'Unknown'
|
_UNKNOWN = 'Unknown'
|
||||||
LOGGER = logging.getLogger("wideq.client")
|
LOGGER = logging.getLogger("wideq.client")
|
||||||
@ -138,6 +139,24 @@ class Client(object):
|
|||||||
return device
|
return device
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def get_device_obj(self, device_id):
|
||||||
|
"""Look up a subclass of Device object by device ID.
|
||||||
|
|
||||||
|
Return a Device instance if no subclass exists for the device type.
|
||||||
|
Return None if the device does not exist.
|
||||||
|
"""
|
||||||
|
from . import util
|
||||||
|
|
||||||
|
device_info = self.get_device(device_id)
|
||||||
|
if not device_info:
|
||||||
|
return None
|
||||||
|
classes = util.device_classes()
|
||||||
|
if device_info.type in classes:
|
||||||
|
return classes[device_info.type](self, device_info)
|
||||||
|
LOGGER.debug('No specific subclass for deviceType %s, using default',
|
||||||
|
device_info.type)
|
||||||
|
return Device(self, device_info)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls, state: Dict[str, Any]) -> 'Client':
|
def load(cls, state: Dict[str, Any]) -> 'Client':
|
||||||
"""Load a client from serialized state.
|
"""Load a client from serialized state.
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from typing import TypeVar
|
from typing import TypeVar
|
||||||
|
|
||||||
from .client import Device
|
from .client import Device, DeviceType
|
||||||
|
|
||||||
|
|
||||||
T = TypeVar('T', bound=Device)
|
T = TypeVar('T', bound=Device)
|
||||||
@ -29,3 +29,22 @@ def lookup_reference(attr: str, data: dict, device: T) -> str:
|
|||||||
if value is None:
|
if value is None:
|
||||||
return 'Off'
|
return 'Off'
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def device_classes():
|
||||||
|
"""The mapping of every Device subclass related to the DeviceType enum
|
||||||
|
"""
|
||||||
|
from .ac import ACDevice
|
||||||
|
from .dryer import DryerDevice
|
||||||
|
from .dishwasher import DishWasherDevice
|
||||||
|
from .washer import WasherDevice
|
||||||
|
from .refrigerator import RefrigeratorDevice
|
||||||
|
|
||||||
|
return {
|
||||||
|
DeviceType.AC: ACDevice,
|
||||||
|
DeviceType.KIMCHI_REFRIGERATOR: RefrigeratorDevice,
|
||||||
|
DeviceType.REFRIGERATOR: RefrigeratorDevice,
|
||||||
|
DeviceType.DISHWASHER: DishWasherDevice,
|
||||||
|
DeviceType.DRYER: DryerDevice,
|
||||||
|
DeviceType.WASHER: WasherDevice,
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user