Ejemplo n.º 1
0
def on_sse_event(event_dict: dict):

    # Lookup corresponding OpenHAB event
    event = get_event(event_dict)

    # Update item in registry BEFORE posting to the event bus
    # so the items have the correct state when we process the event in a rule
    try:
        if isinstance(event, HABApp.core.events.ValueUpdateEvent):
            __item = Items.get_item(
                event.name)  # type: HABApp.core.items.base_item.BaseValueItem
            __item.set_value(event.value)
            HABApp.core.EventBus.post_event(event.name, event)
            return None
        elif isinstance(event, HABApp.openhab.events.ThingStatusInfoEvent):
            __thing = Items.get_item(
                event.name)  # type: HABApp.openhab.items.Thing
            __thing.process_event(event)
            HABApp.core.EventBus.post_event(event.name, event)
            return None
    except HABApp.core.Items.ItemNotFoundException:
        pass

    # Events which change the ItemRegistry
    if isinstance(event, (HABApp.openhab.events.ItemAddedEvent,
                          HABApp.openhab.events.ItemUpdatedEvent)):
        item = map_item(event.name, event.type, 'NULL')
        if item is None:
            return None

        # check already existing item so we can print a warning if something changes
        try:
            existing_item = Items.get_item(item.name)
            if isinstance(existing_item, item.__class__):
                # it's the same item class so we don't replace it!
                item = existing_item
            else:
                log.warning(
                    f'Item changed type from {existing_item.__class__} to {item.__class__}'
                )
        except Items.ItemNotFoundException:
            pass

        # always overwrite with new definition
        Items.set_item(item)

    elif isinstance(event, HABApp.openhab.events.ItemRemovedEvent):
        Items.pop_item(event.name)

    # Send Event to Event Bus
    HABApp.core.EventBus.post_event(event.name, event)
    return None
Ejemplo n.º 2
0
    def test_item(self):

        NAME = 'test'
        created_item = Item(NAME)
        Items.set_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(), [])
Ejemplo n.º 3
0
    async def on_connect_function(self):
        data = await async_get_items()
        if data is None:
            return None

        found_items = len(data)
        for _dict in data:
            item_name = _dict['name']
            new_item = map_item(item_name, _dict['type'], _dict['state'])
            if new_item is None:
                continue

            try:
                # if the item already exists and it has the correct type just update its state
                # Since we load the items before we load the rules this should actually never happen
                existing_item = Items.get_item(
                    item_name)  # type: HABApp.core.items.BaseValueItem
                if isinstance(existing_item, new_item.__class__):
                    existing_item.set_value(
                        new_item.value
                    )  # use the converted state from the new item here
                    new_item = existing_item
            except Items.ItemNotFoundException:
                pass

            # create new item or change item type
            Items.set_item(new_item)

        # remove items which are no longer available
        ist = set(Items.get_all_item_names())
        soll = {k['name'] for k in data}
        for k in ist - soll:
            if isinstance(Items.get_item(k), HABApp.openhab.items.OpenhabItem):
                Items.pop_item(k)

        log.info(f'Updated {found_items:d} Items')

        # try to update things, too
        data = await async_get_things()
        if data is None:
            return None

        Thing = HABApp.openhab.items.Thing
        for t_dict in data:
            name = t_dict['UID']
            try:
                thing = HABApp.core.Items.get_item(name)
                if not isinstance(thing, Thing):
                    log.warning(
                        f'Item {name} has the wrong type ({type(thing)}), expected Thing'
                    )
                    thing = Thing(name)
            except HABApp.core.Items.ItemNotFoundException:
                thing = Thing(name)

            thing.status = t_dict['statusInfo']['status']
            HABApp.core.Items.set_item(thing)

        # remove things which were deleted
        ist = set(HABApp.core.Items.get_all_item_names())
        soll = {k['UID'] for k in data}
        for k in ist - soll:
            if isinstance(HABApp.core.Items.get_item(k), Thing):
                HABApp.core.Items.pop_item(k)

        log.info(f'Updated {len(data):d} Things')
        return None