def test_pop(clean_reg): Items.add_item(Item('test')) assert Items.item_exists('test') with pytest.raises(Items.ItemNotFoundException): Items.pop_item('asdfadsf') Items.pop_item('test') assert not Items.item_exists('test')
def test_retain_create(): topic = '/test/creation' assert not Items.item_exists(topic) process_msg(None, None, MqttDummyMsg(topic, 'aaa', retain=False)) assert not Items.item_exists(topic) # Retain True will create the item process_msg(None, None, MqttDummyMsg(topic, 'adsf123', retain=True)) assert Items.item_exists(topic) assert Items.get_item(topic).value == 'adsf123' Items.pop_item(topic)
def add_to_registry(item: 'HABApp.openhab.items.OpenhabItem', set_value=False): name = item.name for grp in item.groups: MEMBERS.setdefault(grp, set()).add(name) if not Items.item_exists(name): return Items.add_item(item) existing = Items.get_item(name) if isinstance(existing, item.__class__): # If we load directly through the API and not through an event we have to set the value if set_value: existing.set_value(item.value) # remove old groups for grp in set(existing.groups) - set(item.groups): MEMBERS.get(grp, set()).discard(name) # same type - it was only an item update (e.g. label)! existing.tags = item.tags existing.groups = item.groups return None log_warning( log, f'Item type changed from {existing.__class__} to {item.__class__}') # Replace existing item with the updated definition Items.pop_item(name) Items.add_item(item)
def test_factories(self): cls = self.CLS ITEM_NAME = 'testitem' if Items.item_exists(ITEM_NAME): Items.pop_item(ITEM_NAME) c = cls.get_create_item(name=ITEM_NAME, **self.TEST_CREATE_ITEM) assert isinstance(c, cls) assert isinstance(cls.get_item(name=ITEM_NAME), cls)
def test_add(clean_reg): added = Item('test') Items.add_item(added) assert Items.item_exists('test') # adding the same item multiple times will not cause an exception Items.add_item(added) Items.add_item(added) # adding a new item -> exception with pytest.raises(Items.ItemAlreadyExistsError): Items.add_item(Item('test'))
def remove_from_registry(name: str): if not Items.item_exists(name): return None item = Items.get_item(name) # type: HABApp.openhab.items.OpenhabItem for grp in item.groups: MEMBERS.get(grp, set()).discard(name) if isinstance(item, HABApp.openhab.items.GroupItem): MEMBERS.pop(name, None) Items.pop_item(name) return None
def test_item(self): NAME = 'test' created_item = Item(NAME) Items.add_item(created_item) self.assertTrue(Items.item_exists(NAME)) self.assertIs(created_item, Items.get_item(NAME)) self.assertEqual(Items.get_all_item_names(), [NAME]) self.assertEqual(Items.get_all_items(), [created_item]) self.assertIs(created_item, Items.pop_item(NAME)) self.assertEqual(Items.get_all_items(), [])