Skip to content

Commit

Permalink
Accept DateTimeItem as input for to_java_zoneddatetime. [openhab-sc…
Browse files Browse the repository at this point in the history
…ripters#4]

Before it would complain with TypeError: Unknown type: <type 'org.openhab.core.library.items.DateTimeItem'>. It turns out that you cannot simply pass it a DateTimeItem, but instead you should pass it its state. The current implementation also accepts passing the item.
  • Loading branch information
conan747 authored and CrazyIvan359 committed Feb 6, 2021
1 parent a113eb1 commit f4d0f56
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Core/automation/lib/python/core/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@
try:
# OH2.x compat1x or OH3
from org.openhab.core.library.types import DateTimeType
from org.openhab.core.library.items import DateTimeItem
except:
DateTimeType = None
DateTimeItem = None

try:
from org.eclipse.smarthome.core.library.types import DateTimeType as EclipseDateTimeType
from org.eclipse.smarthome.core.library.items import DateTimeItem as EclipseDateTimeItem
except:
EclipseDateTimeType = None
EclipseDateTimeItem = None

if 'org.eclipse.smarthome.automation' in sys.modules or 'org.openhab.core.automation' in sys.modules:
# Workaround for Jython JSR223 bug where dates and datetimes are converted
Expand Down Expand Up @@ -277,6 +281,12 @@ def to_java_zoneddatetime(value):
else:
# OH3
return value.getZonedDateTime()
# Eclipse Smarthome DateTimeItem
if EclipseDateTimeItem and isinstance(value, EclipseDateTimeItem):
return to_java_zoneddatetime(value.getState())
# openHAB DateTimeItem
if DateTimeItem and isinstance(value, DateTimeItem):
return to_java_zoneddatetime(value.getState())

raise TypeError("Unknown type: {}".format(str(type(value))))

Expand Down
1 change: 1 addition & 0 deletions Ivan's Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This change log will keep track of what has been done in the `ivans-updates` bra
* **Python**
* Custom logger class and simplier `getLogger` function that automatically prepends the `LOG_PREFIX`.
* OH3.x support while maintaining backwards compatibility with OH2.x.
* Date functions can now accept DateTime Items.

## Changed

Expand Down

0 comments on commit f4d0f56

Please sign in to comment.