1
0
mirror of https://github.com/no2chem/wideq.git synced 2025-05-16 15:20:09 -07:00

Used namedtuples for value info

This commit is contained in:
Adrian Sampson 2018-01-21 12:47:17 -05:00
parent 844d1c1bdb
commit 9bf03ddb46
2 changed files with 28 additions and 17 deletions

View File

@ -33,12 +33,19 @@ def example_command(client, args):
res = mon.poll() res = mon.poll()
if res: if res:
for key, value in res.items(): for key, value in res.items():
if key == 'Operation': try:
options = model.enum('Operation') desc = model.value(key)
print('- {}: {}'.format(key, options[value])) except KeyError:
else:
print('- {}: {}'.format(key, value)) print('- {}: {}'.format(key, value))
if isinstance(desc, wideq.EnumValue):
print('- {}: {}'.format(
key, desc.options.get(value, value)
))
elif isinstance(desc, wideq.RangeValue):
print('- {0}: {1} ({2.min}-{2.max})'.format(
key, value, desc,
))
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass

View File

@ -6,6 +6,7 @@ import json
import hashlib import hashlib
import hmac import hmac
import datetime import datetime
from collections import namedtuple
GATEWAY_URL = 'https://kic.lgthinq.com:46030/api/common/gatewayUriList' GATEWAY_URL = 'https://kic.lgthinq.com:46030/api/common/gatewayUriList'
@ -501,6 +502,10 @@ class DeviceInfo(object):
return requests.get(self.model_info_url).json() return requests.get(self.model_info_url).json()
EnumValue = namedtuple('EnumValue', ['options'])
RangeValue = namedtuple('RangeValue', ['min', 'max', 'step'])
class ModelInfo(object): class ModelInfo(object):
"""A description of a device model's capabilities. """A description of a device model's capabilities.
""" """
@ -508,21 +513,20 @@ class ModelInfo(object):
def __init__(self, data): def __init__(self, data):
self.data = data self.data = data
def enum(self, name): def value(self, name):
"""Get the mapping of options for a given enumerated value. """Look up information about a value.
Return either an `EnumValue` or a `RangeValue`.
""" """
d = self.data['Value'][name] d = self.data['Value'][name]
assert d['type'] == 'Enum' if d['type'] in ('Enum', 'enum'):
return d['option'] return EnumValue(d['option'])
elif d['type'] == 'Range':
def range(self, name): return RangeValue(
"""Get the minimum, maximum, and step for a numeric range value. d['option']['min'], d['option']['max'], d['option']['step']
""" )
else:
d = self.data['Value'][name] assert False, "unsupported value type {}".format(d['type'])
assert d['type'] == 'Range'
return d['option']['min'], d['option']['max'], d['option']['step']
def default(self, name): def default(self, name):
"""Get the default value, if it exists, for a given value. """Get the default value, if it exists, for a given value.