def test_img_size(self): # start with 200k _b1 = b'0x00' * 200 * 1024 _b2 = b'0xFF' * 200 * 1024 with OpenhabTmpItem(None, 'Image') as item, ItemWaiter(OpenhabItem.get_item(item.name)) as item_waiter, \ EventWaiter(item.name, ItemStateChangedEvent) as event_waiter: k = 383 _b1 = b'\xFF\xD8\xFF' + b'\x00' * (1024 - 3) + b'\x00' * (k - 1) * 1024 _b2 = b'\xFF\xD8\xFF' + b'\xFF' * (1024 - 3) + b'\x00' * (k - 1) * 1024 item.oh_post_update(_b1) event_waiter.wait_for_event(_b1) item_waiter.wait_for_state(_b1) item.oh_post_update(_b2) event_waiter.wait_for_event(_b2) item_waiter.wait_for_state(_b2) assert event_waiter.last_event.value == _b2 assert event_waiter.last_event.old_value == _b1 log.info(f'Image with {len(_b2) / 1024 :.0f}k ok!') test_ok = item_waiter.states_ok and event_waiter.events_ok return test_ok
def test_quantity_type_events(self, dimension): unit_of_dimension = { 'Length': 'm', 'Temperature': '°C', 'Pressure': 'hPa', 'Speed': 'km/h', 'Intensity': 'W/m²', 'Angle': '°', 'Dimensionless': '', } item_name = f'{dimension}_event_test' with OpenhabTmpItem(item_name, f'Number:{dimension}') as item, \ EventWaiter(item_name, ValueUpdateEvent) as event_watier, \ ItemWaiter(item) as item_waiter: for state in get_openhab_test_states('Number'): self.openhab.post_update( item_name, f'{state} {unit_of_dimension[dimension]}') event_watier.wait_for_event(state) item_waiter.wait_for_state(state) all_events_ok = event_watier.events_ok return all_events_ok
def test_sw_mode_inverted(self): mm = MultiModeItem.get_create_item(get_random_name()) with OpenhabTmpItem(None, 'Switch') as switch, ItemWaiter( OpenhabItem.get_item(switch.name)) as waiter: switch.on() waiter.wait_for_state('ON') mode = SwitchItemValueMode('test', switch, invert_switch=True, logger=log) mm.add_mode(0, mode) assert mode.enabled is False, mode.enabled mode.set_value('asdf') assert mode.enabled is False, mode.enabled mode.set_value(0) assert mode.enabled is False, mode.enabled switch.off() waiter.wait_for_state('OFF') assert mode.enabled is True, mode.enabled HABApp.core.Items.pop_item(mm.name)
def create_meta(self): with OpenhabTmpItem(None, 'String') as tmpitem: d = run_coro(async_get_item_with_habapp_meta(tmpitem.name)) assert d['metadata']['HABApp'] is None # create empty set run_coro( async_set_habapp_metadata(tmpitem.name, HABAppThingPluginData())) d = run_coro(async_get_item_with_habapp_meta(tmpitem.name)) assert isinstance(d['metadata']['HABApp'], HABAppThingPluginData) # create valid data run_coro( async_set_habapp_metadata( tmpitem.name, HABAppThingPluginData(created_link='asdf', created_ns=['a', 'b']))) d = run_coro(async_get_item_with_habapp_meta(tmpitem.name)) d = d['metadata']['HABApp'] assert isinstance(d, HABAppThingPluginData) assert d.created_link == 'asdf' assert d.created_ns == ['a', 'b'] # remove metadata again run_coro(async_remove_habapp_metadata(tmpitem.name)) d = run_coro(async_get_item_with_habapp_meta(tmpitem.name)) assert d['metadata']['HABApp'] is None return True
def test_func(self, item_type, test_params): # create a nice name for the tmp item item_type = str(item_type).split('.')[-1][:-6] item_name = f'{item_type}_item_test' with OpenhabTmpItem(item_type, item_name) as item, ItemWaiter(OpenhabItem.get_item(item_name)) as waiter: for test_param in test_params: assert isinstance(test_param, TestParam) func = getattr(item, test_param.func_name) if test_param.func_params is None: func() else: if isinstance(test_param.func_params, (str, float, int, bytes)): func(test_param.func_params) else: func(*test_param.func_params) if waiter.wait_for_state(test_param.result): log.info(f'{item_type}.{test_param.func_name}() is ok!') # test properties if test_param.func_name not in ('percent', 'oh_post_update', 'oh_send_command'): prop_name = f'is_{test_param.func_name}' assert getattr(item, prop_name)() is True log.info(f'{item_type}.{prop_name}() is ok!') # reset state so we don't get false positives item.set_value(None)
def test_async_oder(self): with OpenhabTmpItem( 'String', 'AsyncOrderTest') as item, ItemWaiter(item) as waiter: for _ in range(10): for i in range(0, 5): item.oh_post_update(i) waiter.wait_for_state('4')
def test_func(self, item_type, func_name, test_vals): with OpenhabTmpItem(item_type) as tmpitem, ItemWaiter(OpenhabItem.get_item(tmpitem.name)) as waiter: for val in test_vals: getattr(tmpitem, func_name)(val) waiter.wait_for_state(val) for val in test_vals: tmpitem.set_value(val) getattr(tmpitem, func_name)() waiter.wait_for_state(val)
def change_item(self): with OpenhabTmpItem(None, 'Number') as tmpitem: NumberItem.get_item(tmpitem.name) create_item('String', tmpitem.name) EventWaiter(tmpitem.name, ItemUpdatedEvent(tmpitem.name, 'String'), 2, False) StringItem.get_item(tmpitem.name) create_item('DateTime', tmpitem.name) EventWaiter(tmpitem.name, ItemUpdatedEvent(tmpitem.name, 'DateTime'), 2, False) DatetimeItem.get_item(tmpitem.name)
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 change_item(self): with OpenhabTmpItem('Number') as tmpitem: NumberItem.get_item(tmpitem.name) with EventWaiter(tmpitem.name, ItemUpdatedEvent, 2) as e: create_item('String', tmpitem.name) e.wait_for_event(type='String', name=tmpitem.name) StringItem.get_item(tmpitem.name) with EventWaiter(tmpitem.name, ItemUpdatedEvent, 2) as e: create_item('DateTime', tmpitem.name) e.wait_for_event(type='DateTime', name=tmpitem.name) DatetimeItem.get_item(tmpitem.name)
def test_events(self, item_type, test_values): item_name = f'{item_type}_value_test' with OpenhabTmpItem(item_type, item_name), EventWaiter(item_name, ValueUpdateEvent) as waiter: for value in test_values: self.openhab.post_update(item_name, value) waiter.wait_for_event(value=value) # Contact does not support commands if item_type != 'Contact': self.openhab.send_command(item_name, value) waiter.wait_for_event(value=value)
def test_post_update(self, oh_type, values): if isinstance(values, str): values = [values] with OpenhabTmpItem(oh_type) as item, ItemWaiter(item) as waiter: for value in values: self.openhab.post_update(item, value) waiter.wait_for_state(value) for value in values: if oh_type != 'Contact': self.openhab.send_command(item, value) waiter.wait_for_state(value)
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 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()
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!')
def test_img_size(self): # start with 200k _b1 = b'0x00' * 200 * 1024 _b2 = b'0xFF' * 200 * 1024 with OpenhabTmpItem('Image') as item, ItemWaiter(OpenhabItem.get_item(item.name)) as item_waiter, \ EventWaiter(item.name, ItemStateChangedEvent) as event_waiter: k = 383 _b1 = b'\xFF\xD8\xFF' + b'\x00' * (1024 - 3) + b'\x00' * (k - 1) * 1024 _b2 = b'\xFF\xD8\xFF' + b'\xFF' * (1024 - 3) + b'\x00' * (k - 1) * 1024 item.oh_post_update(_b1) event_waiter.wait_for_event(value=_b1) item_waiter.wait_for_state(_b1) item.oh_post_update(_b2) event_waiter.wait_for_event(value=_b2, old_value=_b1) item_waiter.wait_for_state(_b2) log.info(f'Image with {len(_b2) / 1024 :.0f}k ok!')
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()
def test_metadata(self): with OpenhabTmpItem(None, 'String') as item: self.openhab.set_metadata(item, 'MyNameSpace', 'MyValue', {'key': 'value'}) self.openhab.remove_metadata(item, 'MyNameSpace')
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()