示例#1
0
def test_datetime():
    # Todo: remove this test once we go >= OH3.1
    # Old format
    assert map_item('test1', 'DateTime', '2018-11-19T09:47:38.284+0000', frozenset(), frozenset(), {}) == \
           DatetimeItem('test', datetime(2018, 11, 19,  9, 47, 38, 284000)) or \
           DatetimeItem('test', datetime(2018, 11, 19, 10, 47, 38, 284000))

    # From >= OH3.1
    assert map_item('test1', 'DateTime', '2021-04-10T21:00:43.043996+0000', frozenset(), frozenset(), {}) == \
           DatetimeItem('test', datetime(2021, 4, 10, 21, 0, 43, 43996)) or \
           DatetimeItem('test', datetime(2021, 4, 10, 23, 0, 43, 43996))
示例#2
0
    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)
示例#3
0
    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)
示例#4
0
    def __init__(self):
        super().__init__()

        # get items
        test_contact = ContactItem.get_item('TestContact')
        test_date_time = DatetimeItem.get_item('TestDateTime')
        test_switch = SwitchItem.get_item('TestSwitch')

        # Trigger on item updates
        test_contact.listen_event(self.item_state_update, ItemStateEvent)
        test_date_time.listen_event(self.item_state_update, ValueUpdateEvent)

        # Trigger on item changes
        test_contact.listen_event(self.item_state_change, ItemStateChangedEvent)
        test_date_time.listen_event(self.item_state_change, ValueChangeEvent)

        # Trigger on item commands
        test_switch.listen_event(self.item_command, ItemCommandEvent)
示例#5
0
def map_items(name, openhab_type: str, openhab_value: str):
    assert isinstance(openhab_type, str), type(openhab_type)
    assert isinstance(openhab_value, str), type(openhab_value)

    value: typing.Optional[str] = openhab_value
    if openhab_value == 'NULL' or openhab_value == 'UNDEF':
        value = None

    # Quantity types are like this: Number:Temperature and have a unit set: "12.3 °C".
    # We have to remove the dimension from the type and remove the unit from the value
    if ':' in openhab_type:
        openhab_type = openhab_type[:openhab_type.find(':')]
        # if the item is not initialized its None and has no dimension
        if value is not None:
            value, _ = QuantityValue.split_unit(value)

    # Specific classes
    if openhab_type == "Switch":
        return SwitchItem(name, value)

    if openhab_type == "String":
        return StringItem(name, value)

    if openhab_type == "Contact":
        return ContactItem(name, value)

    if openhab_type == "Rollershutter":
        if value is None:
            return RollershutterItem(name, value)
        return RollershutterItem(name, float(value))

    if openhab_type == "Dimmer":
        return DimmerItem(name, value)

    if openhab_type == "Number":
        if value is None:
            return NumberItem(name, value)

        # Number items can be int or float
        try:
            return NumberItem(name, int(value))
        except ValueError:
            return NumberItem(name, float(value))

    if openhab_type == "DateTime":
        if value is None:
            return DatetimeItem(name, value)
        dt = datetime.datetime.strptime(value.replace('+', '000+'),
                                        '%Y-%m-%dT%H:%M:%S.%f%z')
        # all datetimes from openhab have a timezone set so we can't easily compare them
        # --> TypeError: can't compare offset-naive and offset-aware datetimes
        dt = dt.astimezone(
            tz=None)  # Changes datetime object so it uses system timezone
        dt = dt.replace(tzinfo=None)  # Removes timezone awareness
        return DatetimeItem(name, dt)

    if openhab_type == "Color":
        if value is None:
            return ColorItem(name)
        return ColorItem(name, *(float(k) for k in value.split(',')))

    if openhab_type == "Location":
        return LocationItem(name, value)

    if openhab_type == "Player":
        return PlayerItem(name, value)

    if openhab_type == "Group":
        return GroupItem(name, value)

    if openhab_type == "Image":
        img = ImageItem(name)
        if value is None:
            return img
        img.set_value(RawValue(value))
        return img

    raise ValueError(f'Unknown Openhab type: {openhab_type}')
示例#6
0
def map_item(name: str, type: str, value: Optional[str],
             tags: FrozenSet[str], groups: FrozenSet[str], metadata: Optional[Dict[str, Dict[str, Any]]]) -> \
        Optional['HABApp.openhab.items.OpenhabItem']:
    try:
        assert isinstance(type, str)
        assert value is None or isinstance(value, str)

        if value == 'NULL' or value == 'UNDEF':
            value = None

        # map Metadata
        if metadata is not None:
            meta = Map({k: MetaData(v['value'], Map(v.get('config', {}))) for k, v in metadata.items()})
        else:
            meta = Map()

        # Quantity types are like this: Number:Temperature and have a unit set: "12.3 °C".
        # We have to remove the dimension from the type and remove the unit from the value
        if ':' in type:
            type, dimension = type.split(':')
            # if the item is not initialized its None and has no dimension
            if value is not None:
                value, _ = QuantityValue.split_unit(value)

        # Specific classes
        if type == "Switch":
            return SwitchItem(name, value, tags=tags, groups=groups, metadata=meta)

        if type == "String":
            return StringItem(name, value, tags=tags, groups=groups, metadata=meta)

        if type == "Contact":
            return ContactItem(name, value, tags=tags, groups=groups, metadata=meta)

        if type == "Rollershutter":
            if value is None:
                return RollershutterItem(name, value, tags=tags, groups=groups, metadata=meta)
            return RollershutterItem(name, float(value), tags=tags, groups=groups, metadata=meta)

        if type == "Dimmer":
            if value is None:
                return DimmerItem(name, value, tags=tags, groups=groups, metadata=meta)
            return DimmerItem(name, float(value), tags=tags, groups=groups, metadata=meta)

        if type == "Number":
            if value is None:
                return NumberItem(name, value, tags=tags, groups=groups, metadata=meta)

            # Number items can be int or float
            try:
                return NumberItem(name, int(value), tags=tags, groups=groups, metadata=meta)
            except ValueError:
                return NumberItem(name, float(value), tags=tags, groups=groups, metadata=meta)

        if type == "DateTime":
            if value is None:
                return DatetimeItem(name, value, tags=tags, groups=groups, metadata=meta)
            # Todo: remove this once we go >= OH3.1
            # Previous OH versions used a datetime string like this:
            # 2018-11-19T09:47:38.284+0100
            # OH 3.1 uses
            # 2021-04-10T22:00:43.043996+0200
            if len(value) == 28:
                value = value.replace('+', '000+')
            dt = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%f%z')
            # all datetimes from openhab have a timezone set so we can't easily compare them
            # --> TypeError: can't compare offset-naive and offset-aware datetimes
            dt = dt.astimezone(tz=None)   # Changes datetime object so it uses system timezone
            dt = dt.replace(tzinfo=None)  # Removes timezone awareness
            return DatetimeItem(name, dt, tags=tags, groups=groups, metadata=meta)

        if type == "Color":
            if value is None:
                return ColorItem(name, tags=tags, groups=groups, metadata=meta)
            return ColorItem(name, *(float(k) for k in value.split(',')), tags=tags, groups=groups, metadata=meta)

        if type == "Image":
            img = ImageItem(name, tags=tags, groups=groups, metadata=meta)
            if value is None:
                return img
            img.set_value(RawValue(value))
            return img

        if type == "Group":
            return GroupItem(name, value, tags=tags, groups=groups, metadata=meta)

        if type == "Location":
            return LocationItem(name, value, tags=tags, groups=groups, metadata=meta)

        if type == "Player":
            return PlayerItem(name, value, tags=tags, groups=groups, metadata=meta)

        raise ValueError(f'Unknown Openhab type: {type} for {name}')

    except Exception as e:
        process_exception('map_items', e, logger=log)
        return None