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

Allow strings in ModelInfo

The only instance where this is used is a comment,
see issue #62. For now it will just be ignored. The comment seems to
contain several times.

Fixes #62
This commit is contained in:
Frederik Gladhorn 2020-01-14 14:43:32 +01:00 committed by Frederik Gladhorn
parent d8389f3bee
commit be05e1b33e
2 changed files with 23 additions and 9 deletions

View File

@ -1,7 +1,7 @@
import unittest import unittest
from wideq.client import ( from wideq.client import (
BitValue, EnumValue, ModelInfo, RangeValue, ReferenceValue) BitValue, EnumValue, ModelInfo, RangeValue, ReferenceValue, StringValue)
DATA = { DATA = {
@ -66,10 +66,15 @@ DATA = {
], ],
'type': 'Bit' 'type': 'Bit'
}, },
'TimeBsOn': {
'_comment':
'오전 12시 30분은 0030, 오후12시30분은 1230 ,오후 4시30분은 1630 off는 0 ',
'type': 'String'
},
'Unexpected': {'type': 'Unexpected'}, 'Unexpected': {'type': 'Unexpected'},
'StringOption': { 'Unexpected2': {
'type': 'String', 'type': 'Unexpected',
'option': 'some string' 'option': 'some option'
}, },
}, },
'Course': { 'Course': {
@ -120,6 +125,12 @@ class ModelInfoTest(unittest.TestCase):
expected = ReferenceValue(DATA['Course']) expected = ReferenceValue(DATA['Course'])
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
def test_string(self):
actual = self.model_info.value('TimeBsOn')
expected = StringValue(
"오전 12시 30분은 0030, 오후12시30분은 1230 ,오후 4시30분은 1630 off는 0 ")
self.assertEqual(expected, actual)
def test_value_unsupported(self): def test_value_unsupported(self):
data = "{'type': 'Unexpected'}" data = "{'type': 'Unexpected'}"
with self.assertRaisesRegex( with self.assertRaisesRegex(
@ -129,9 +140,9 @@ class ModelInfoTest(unittest.TestCase):
self.model_info.value('Unexpected') self.model_info.value('Unexpected')
def test_value_unsupported_but_data_available(self): def test_value_unsupported_but_data_available(self):
data = "{'type': 'String', 'option': 'some string'}" data = "{'type': 'Unexpected', 'option': 'some option'}"
with self.assertRaisesRegex( with self.assertRaisesRegex(
ValueError, ValueError,
f"unsupported value name: 'StringOption'" f"unsupported value name: 'Unexpected2'"
f" type: 'String' data: '{data}"): f" type: 'Unexpected' data: '{data}"):
self.model_info.value('StringOption') self.model_info.value('Unexpected2')

View File

@ -292,6 +292,7 @@ RangeValue = namedtuple('RangeValue', ['min', 'max', 'step'])
#: This is a value that is a reference to another key in the data that is at #: This is a value that is a reference to another key in the data that is at
#: the same level as the `Value` key. #: the same level as the `Value` key.
ReferenceValue = namedtuple('ReferenceValue', ['reference']) ReferenceValue = namedtuple('ReferenceValue', ['reference'])
StringValue = namedtuple('StringValue', ['comment'])
class ModelInfo(object): class ModelInfo(object):
@ -306,7 +307,7 @@ class ModelInfo(object):
:param name: The name to look up. :param name: The name to look up.
:returns: One of (`BitValue`, `EnumValue`, `RangeValue`, :returns: One of (`BitValue`, `EnumValue`, `RangeValue`,
`ReferenceValue`). `ReferenceValue`, `StringValue`).
:raises ValueError: If an unsupported type is encountered. :raises ValueError: If an unsupported type is encountered.
""" """
d = self.data['Value'][name] d = self.data['Value'][name]
@ -323,6 +324,8 @@ class ModelInfo(object):
elif d['type'].lower() == 'reference': elif d['type'].lower() == 'reference':
ref = d['option'][0] ref = d['option'][0]
return ReferenceValue(self.data[ref]) return ReferenceValue(self.data[ref])
elif d['type'].lower() == 'string':
return StringValue(d.get('_comment', ''))
else: else:
raise ValueError( raise ValueError(
f"unsupported value name: '{name}'" f"unsupported value name: '{name}'"