def add_item_to_grp(self): new_item = OpenhabTmpItem('Switch') try: with EventWaiter(self.group.name, ItemUpdatedEvent) as w: new_item.create_item(groups=[self.group.name]) event = w.wait_for_event() while event.name != self.group.name: w.wait_for_event() except TestCaseFailed: return None finally: new_item.remove() raise TestCaseFailed( f'Event for group {self.group.name} reveived but expected none!')
class TestOpenhabGroupFunction(TestBaseRule): def __init__(self): super().__init__() self.group = OpenhabTmpItem('Group') self.item1 = OpenhabTmpItem('Switch') self.item2 = OpenhabTmpItem('Switch') self.add_test('Group function', self.test_group_update) self.add_test('Group member change', self.add_item_to_grp) def set_up(self): self.item1.create_item(groups=[self.group.name]) self.item2.create_item(groups=[self.group.name]) self.group.create_item(group_type='Switch', group_function='OR', group_function_params=['ON', 'OFF']) def tear_down(self): self.item1.remove() self.item2.remove() self.group.remove() def test_group_update(self): item1 = SwitchItem.get_item(self.item1.name) item2 = SwitchItem.get_item(self.item2.name) group = GroupItem.get_item(self.group.name) with ItemWaiter(group) as waiter: waiter.wait_for_state(None) item1.oh_post_update('ON') waiter.wait_for_state('ON') item1.oh_post_update('OFF') waiter.wait_for_state('OFF') item2.oh_post_update('ON') waiter.wait_for_state('ON') def add_item_to_grp(self): new_item = OpenhabTmpItem('Switch') try: with EventWaiter(self.group.name, ItemUpdatedEvent) as w: new_item.create_item(groups=[self.group.name]) event = w.wait_for_event() while event.name != self.group.name: w.wait_for_event() except TestCaseFailed: return None finally: new_item.remove() raise TestCaseFailed( f'Event for group {self.group.name} reveived but expected none!')
class OpenhabItems(TestBaseRule): def __init__(self): super().__init__() self.add_test('ApiDoc', self.test_api) self.add_test('MemberTags', self.test_tags) self.add_test('MemberGroups', self.test_groups) self.add_test('TestExisting', self.test_existing) self.item_number = OpenhabTmpItem('Number') self.item_switch = OpenhabTmpItem('Switch') self.item_group = OpenhabTmpItem('Group') self.item_string = OpenhabTmpItem('String') def set_up(self): self.item_number.create_item(label='No metadata') self.item_switch.create_item() self.openhab.set_metadata(self.item_switch.name, 'homekit', 'HeatingThresholdTemperature', { 'minValue': 0.5, 'maxValue': 20 }) self.item_group.create_item( label='MyGrpValue [%s]', category='text', tags=['DocItem'], group_function='AND', group_function_params=['VALUE_TRUE', 'VALUE_FALSE']) self.item_string.create_item(label='MyStrValue [%s]', category='text', tags=['DocItem'], groups=[self.item_group.name]) self.openhab.set_metadata(self.item_string.name, 'ns1', 'v1', { 'key11': 'value11', 'key12': 'value12' }) self.openhab.set_metadata(self.item_string.name, 'ns2', 'v2', {'key2': 'value2'}) self.openhab.set_metadata(self.item_group.name, 'ns3', 'v3', {}) def tear_down(self): self.item_string.remove() self.item_switch.remove() def test_existing(self): item = StringItem.get_item('TestString') assert item.tags == frozenset(['TestTag']) assert item.groups == frozenset(['TestGroup']) assert list(item.metadata.keys()) == ['meta1'] assert item.metadata['meta1'].value == 'test' assert item.metadata['meta1'].config == Map({'key': 'value'}) def test_api(self): self.openhab.get_item(self.item_string.name) self.openhab.get_item(self.item_number.name, all_metadata=True) self.openhab.get_item(self.item_string.name, all_metadata=True) self.openhab.get_item(self.item_switch.name, all_metadata=True) self.openhab.get_item(self.item_group.name, all_metadata=True) asyncio.run_coroutine_threadsafe(async_get_items(all_metadata=True), loop).result() @OpenhabTmpItem.use('String', arg_name='oh_item') def test_tags(self, oh_item: OpenhabTmpItem): oh_item.create_item(tags=['tag1', 'tag2']) item = StringItem.get_item(oh_item.name) assert item.tags == {'tag1', 'tag2'} oh_item.modify(tags=['tag1', 'tag4']) assert item.tags == {'tag1', 'tag4'} oh_item.modify() assert item.tags == set() @OpenhabTmpItem.use('String', arg_name='oh_item') @OpenhabTmpItem.create('Group', 'group1') @OpenhabTmpItem.create('Group', 'group2') def test_groups(self, oh_item: OpenhabTmpItem): grp1 = GroupItem.get_item('group1') grp2 = GroupItem.get_item('group2') assert grp1.members == tuple() assert grp2.members == tuple() oh_item.create_item(groups=['group1']) item = StringItem.get_item(oh_item.name) assert item.groups == {'group1'} assert grp1.members == (item, ) oh_item.modify(groups=['group1', 'group2']) assert item.groups == {'group1', 'group2'} assert grp1.members == (item, ) assert grp2.members == (item, ) oh_item.modify() assert item.groups == set() assert grp1.members == tuple() assert grp2.members == tuple()