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 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_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 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_mqtt_events(self, event_type): topic = 'test/event_topic' with EventWaiter(topic, event_type) as waiter: for data in self.mqtt_test_data: self.mqtt.publish(topic, data) waiter.wait_for_event(data) return waiter.events_ok
def _run(self, values, event): 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, self.secs + 2) as w: w.wait_for_event(seconds=self.secs)
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 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_mqtt_pair_item(self): topic_read = 'test/topic_read' topic_write = 'test/topic_write' item = MqttPairItem.get_create_item(topic_read, topic_write) # Ensure we send on the write topic with EventWaiter(topic_write, ValueUpdateEvent) as event_waiter: item.publish('ddddddd') event_waiter.wait_for_event(value='ddddddd') # Read Topic has to be updated properly with ItemWaiter(item) as item_waiter: self.mqtt.publish(topic_read, 'asdfasdf') item_waiter.wait_for_state(item)
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
def item_events(self, changes=False, secs=5, values=[]): watch_item = Item.get_create_item('watch_item', values[0]) event = ValueNoUpdateEvent if not changes else ValueNoChangeEvent def cb(event: ValueNoUpdateEvent): assert event.name == watch_item.name, f'Wrong name: {event.name} != {watch_item.name}' assert event.seconds == secs, f'Wrong seconds: {event.seconds} != {secs}' self.item_watch_and_listen(watch_item.name, secs, cb, changes) for step, value in enumerate(values): watch_item.set_value(value) with EventWaiter(watch_item.name, event, secs + 2) as w: w.wait_for_event(value) if not w.events_ok: return w.events_ok return True
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 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 trigger_event(self): 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) as w: w.wait_for_event(value=123)