From be05e1b33e706e8afd723a2e6839684b91220ed8 Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn Date: Tue, 14 Jan 2020 14:43:32 +0100 Subject: [PATCH] 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 --- tests/test_client.py | 27 +++++++++++++++++++-------- wideq/client.py | 5 ++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index 2aeffe2..a7f1f62 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,7 +1,7 @@ import unittest from wideq.client import ( - BitValue, EnumValue, ModelInfo, RangeValue, ReferenceValue) + BitValue, EnumValue, ModelInfo, RangeValue, ReferenceValue, StringValue) DATA = { @@ -66,10 +66,15 @@ DATA = { ], 'type': 'Bit' }, + 'TimeBsOn': { + '_comment': + '오전 12시 30분은 0030, 오후12시30분은 1230 ,오후 4시30분은 1630 off는 0 ', + 'type': 'String' + }, 'Unexpected': {'type': 'Unexpected'}, - 'StringOption': { - 'type': 'String', - 'option': 'some string' + 'Unexpected2': { + 'type': 'Unexpected', + 'option': 'some option' }, }, 'Course': { @@ -120,6 +125,12 @@ class ModelInfoTest(unittest.TestCase): expected = ReferenceValue(DATA['Course']) 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): data = "{'type': 'Unexpected'}" with self.assertRaisesRegex( @@ -129,9 +140,9 @@ class ModelInfoTest(unittest.TestCase): self.model_info.value('Unexpected') def test_value_unsupported_but_data_available(self): - data = "{'type': 'String', 'option': 'some string'}" + data = "{'type': 'Unexpected', 'option': 'some option'}" with self.assertRaisesRegex( ValueError, - f"unsupported value name: 'StringOption'" - f" type: 'String' data: '{data}"): - self.model_info.value('StringOption') + f"unsupported value name: 'Unexpected2'" + f" type: 'Unexpected' data: '{data}"): + self.model_info.value('Unexpected2') diff --git a/wideq/client.py b/wideq/client.py index 32229e0..8369ae2 100644 --- a/wideq/client.py +++ b/wideq/client.py @@ -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 #: the same level as the `Value` key. ReferenceValue = namedtuple('ReferenceValue', ['reference']) +StringValue = namedtuple('StringValue', ['comment']) class ModelInfo(object): @@ -306,7 +307,7 @@ class ModelInfo(object): :param name: The name to look up. :returns: One of (`BitValue`, `EnumValue`, `RangeValue`, - `ReferenceValue`). + `ReferenceValue`, `StringValue`). :raises ValueError: If an unsupported type is encountered. """ d = self.data['Value'][name] @@ -323,6 +324,8 @@ class ModelInfo(object): elif d['type'].lower() == 'reference': ref = d['option'][0] return ReferenceValue(self.data[ref]) + elif d['type'].lower() == 'string': + return StringValue(d.get('_comment', '')) else: raise ValueError( f"unsupported value name: '{name}'"