Example #1
0
def getLastUpdate(item_or_item_name):
    """
    Returns the Item's last update datetime as an ``org.joda.time.DateTime``.
    If Joda is missing it will return a ``java.time.ZonedDateTime`` instead.

    Args:
        item_or_item_name (Item or str): name of the Item

    Returns:
        DateTime: Joda DateTime representing the time of the Item's last update
        ZonedDateTime: ZonedDateTime representing the time of the Item's last update
    """
    LOG.warn("The 'core.utils.getLastUpdate' function is pending deprecation.")
    try:
        from core.actions import PersistenceExtensions
        item = itemRegistry.getItem(item_or_item_name) if isinstance(
            item_or_item_name, basestring) else item_or_item_name
        last_update = PersistenceExtensions.lastUpdate(item)
        if last_update is not None:
            return to_joda_datetime(
                last_update) if JodaDateTime else to_java_zoneddatetime(
                    last_update)
        LOG.warning(
            u"No existing lastUpdate data for item: '{}', so returning 1970-01-01T00:00:00Z"
            .format(item.name))
    except:
        # There is an issue using the StartupTrigger and saving scripts over SMB, where changes are detected before the file
        # is completely written. The first read breaks because of a partial file write and the second read succeeds.
        LOG.warning(
            u"Exception when getting lastUpdate data for item: '{}', so returning 1970-01-01T00:00:00Z"
            .format(item.name))
    return JodaDateTime(0) if JodaDateTime else ZonedDateTime(0)
Example #2
0
def getItemValue(item_or_item_name, default_value):
    """
    Returns the Item's value if the Item exists and is initialized, otherwise
    returns the default value. ``itemRegistry.getItem`` will return an object
    for uninitialized items, but it has less methods. ``itemRegistry.getItem``
    will throw an ItemNotFoundException if the Item is not in the registry.

    Args:
        item_or_item_name (Item or str): name of the Item
        default_value (int, float, ON, OFF, OPEN, CLOSED, str, DateTime): the default
            value

    Returns:
        int, float, ON, OFF, OPEN, CLOSED, str, DateTime, or None: the state if
            the Item converted to the type of default value, or the default
            value if the Item's state is NULL or UNDEF
    """
    LOG.warn("The 'core.utils.getItemValue' function is pending deprecation.")
    item = itemRegistry.getItem(item_or_item_name) if isinstance(
        item_or_item_name, basestring) else item_or_item_name
    if isinstance(default_value, int):
        return item.state.intValue() if item.state not in [NULL, UNDEF
                                                           ] else default_value
    elif isinstance(default_value, float):
        return item.state.floatValue() if item.state not in [
            NULL, UNDEF
        ] else default_value
    elif default_value in [ON, OFF, OPEN, CLOSED]:
        return item.state if item.state not in [NULL, UNDEF] else default_value
    elif isinstance(default_value, str):
        return item.state.toFullString() if item.state not in [
            NULL, UNDEF
        ] else default_value
    elif JodaDateTime and isinstance(default_value, JodaDateTime):
        # We return a org.joda.time.DateTime from a org.eclipse.smarthome.core.library.types.DateTimeType
        return to_joda_datetime(
            item.state) if item.state not in [NULL, UNDEF] else default_value
    elif isinstance(default_value, ZonedDateTime):
        # We return a java.time.ZonedDateTime
        return to_java_zoneddatetime(
            item.state) if item.state not in [NULL, UNDEF] else default_value
    else:
        LOG.warn("The type of the passed default value is not handled")
        return None
                                                    00, 00,
                                                    ZoneId.systemDefault()),
                                   output='Java',
                                   log=log)

    #Check date was changed
    assert days_between(ZonedDateTime.now(), today_time) == 0, \
                "time object failed to change date for today"
    assert days_between(ZonedDateTime.now(), today_datetime) == 0, \
                "datetime object failed to change date for today"
    assert days_between(ZonedDateTime.now(), today_ZonedDateTime) == 0, \
                "ZonedDateTime object failed to change date for today"
    #Check time wasn't changed
    assert time(22, 59, 59, 500000) <= to_python_datetime(today_time).time() <= time(23, 00, 1, 00), \
                             "time object failed, time has changed"
    assert to_java_zoneddatetime(datetime(2019, 10, 8, 22, 59, 59,0)).toLocalTime() \
                    <= today_datetime.toLocalTime() <= \
                    to_java_zoneddatetime(datetime(2019, 10, 8, 23, 00, 1, 00)).toLocalTime(), \
                    "datetime object failed, time has changed {} {}" \
                    .format(str(to_java_zoneddatetime(datetime(2019, 10, 8, 23, 00, 00)).toLocalTime()), \
                    str(today_datetime.toLocalTime()))
    assert ZonedDateTime.of(2019, 11, 8, 22, 59, 59, 500000, ZoneId.systemDefault()).toLocalTime() \
                    <= today_ZonedDateTime.toLocalTime()<= \
                    ZonedDateTime.of(2019, 11, 8, 23, 00, 1, 00, ZoneId.systemDefault()).toLocalTime() \
                    , "ZonedDateTime object failed, time has changed {} {}" \
                    .format(str(ZonedDateTime.of(2019, 11, 8, 23, 00, 00, 00, ZoneId.systemDefault()).toLocalTime()), \
                    str(today_ZonedDateTime.toLocalTime()))

    #Test other format
    test_dict = {
        'integer: ': int(5000),