def __init__(self): super().__init__() self.group = 'Group_' + get_random_name() self.item1 = 'Item1_' + get_random_name() self.item2 = 'Item2_' + get_random_name() self.add_test('Group Update', self.test_group_update)
def create_meta(self): astro_thing = find_astro_sun_thing() astro_channel = f"{astro_thing}:rise#start" name = get_random_name() # create item and link run_coro( async_create_item('DateTime', name, 'MyCustomLabel', tags=['Tag1'])) run_coro(async_create_channel_link(astro_channel, item_name=name)) # get item run_coro(async_get_item(name)) # delete it run_coro(async_remove_item(name)) time.sleep(0.2) # it still exists but now with editable=False try: run_coro(async_get_item(name)) except ItemNotFoundError: return True return False
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 item_events(self, changes=False, secs=5, values=[]): self.secs = secs self.watch_item = Item.get_create_item(get_random_name()) (self.watch_item.watch_change if changes else self.watch_item.watch_update)(secs) event = ItemNoUpdateEvent if not changes else ItemNoChangeEvent self.ts_set = 0 listener = self.listen_event(self.watch_item, self.check_event, event) for step, value in enumerate(values): if step: time.sleep(0.2) self.ts_set = time.time() self.watch_item.set_value(value) with EventWaiter(self.watch_item.name, event, secs + 2, check_value=False) as w: w.wait_for_event(value) if not w.events_ok: listener.cancel() return w.events_ok listener.cancel() return True
def test_item_create_delete_group(self): test_item = get_random_name('String') test_group = get_random_name('Group') assert not self.openhab.item_exists(test_item) assert not self.openhab.item_exists(test_item) self.openhab.create_item('Group', test_group) assert self.openhab.item_exists(test_group) self.openhab.create_item('String', test_item, groups=[test_group]) assert self.openhab.item_exists(test_item) item = self.openhab.get_item(test_item) self.openhab.get_item(test_group) assert test_group in item.groups self.openhab.remove_item(test_group) self.openhab.remove_item(test_item)
def test_openhab_item_not_found(self): test_item = get_random_name('String') try: self.openhab.get_item(test_item) except Exception as e: if isinstance(e, HABApp.openhab.errors.ItemNotFoundError): return None return 'Exception not raised!'
def test_item_create_delete(self): test_defs = [] for type in get_openhab_test_types(): test_defs.append((type, get_random_name(type))) test_defs.append(('Number', 'HABApp_Ping')) for item_type, item_name in test_defs: assert not self.openhab.item_exists(item_name) self.openhab.create_item(item_type, item_name) assert self.openhab.item_exists(item_name) self.openhab.remove_item(item_name) assert not self.openhab.item_exists(item_name)
def trigger_event(self): self.watch_item = Item.get_create_item(get_random_name()) listener = self.watch_item.listen_event(self.check_event, ValueUpdateEvent) self.run.at(1, HABApp.core.EventBus.post_event, self.watch_item.name, ValueUpdateEvent(self.watch_item.name, 123)) with EventWaiter(self.watch_item.name, ValueUpdateEvent, 2, check_value=True) as w: w.wait_for_event(123) if not w.events_ok: listener.cancel() return w.events_ok listener.cancel() return True
def item_events(self, changes=False, secs=5, values=[]): item_name = get_random_name() self.secs = secs self.watch_item = Item.get_create_item(item_name) watcher = (self.watch_item.watch_change if changes else self.watch_item.watch_update)(secs) event = ItemNoUpdateEvent if not changes else ItemNoChangeEvent listener = self.listen_event(self.watch_item, self.check_event, event) def _run(): self.ts_set = 0 for step, value in enumerate(values): if step: time.sleep(0.2) self.ts_set = time.time() self.watch_item.set_value(value) with EventWaiter(self.watch_item.name, event, secs + 2, check_value=False) as w: w.wait_for_event(value) if not w.events_ok: listener.cancel() return w.events_ok return True if not _run(): return False HABApp.core.Items.pop_item(item_name) assert not HABApp.core.Items.item_exists(item_name) time.sleep(1) self.watch_item = Item.get_create_item(item_name) if not _run(): return False listener.cancel() watcher.cancel() return True
def test_item_change_type(self): test_item = get_random_name('String') assert not self.openhab.item_exists(test_item) self.openhab.create_item('String', test_item) assert self.openhab.item_exists(test_item) # change item type to number and ensure HABApp picks up correctly on the new type self.openhab.create_item('Number', test_item) end = time.time() + 2 while True: time.sleep(0.01) if time.time() > end: HABApp.openhab.items.NumberItem.get_item(test_item) break if isinstance(HABApp.core.Items.get_item(test_item), HABApp.openhab.items.NumberItem): break self.openhab.remove_item(test_item)
def item_events(self, changes=False, secs=5, values=[]): item_name = get_random_name('HABApp') self.watch_item = Item.get_create_item(item_name) self.secs = secs watcher = (self.watch_item.watch_change if changes else self.watch_item.watch_update)(secs) event = ItemNoUpdateEvent if not changes else ItemNoChangeEvent listener = self.listen_event(self.watch_item, self.check_event, event) try: self._run(values, event) HABApp.core.Items.pop_item(item_name) assert not HABApp.core.Items.item_exists(item_name) time.sleep(0.5) self.watch_item = Item.get_create_item(item_name) self._run(values, event) finally: listener.cancel() watcher.cancel() return None
def set_up(self): self.watch_item = Item.get_create_item(get_random_name('HABApp')) self.listener = self.watch_item.listen_event(self.check_event, ValueUpdateEvent)