From ede94354f37d38475d38643f5ff02ab3eda41eda Mon Sep 17 00:00:00 2001 From: Michael Wodniok Date: Thu, 31 Dec 2020 16:48:24 +0100 Subject: [PATCH] [icalendar] Add relevant handling of last_update channel (#9616) Also add migration for older things as compatible. Fixes #9613 Signed-off-by: Michael Wodniok --- .../internal/ICalendarBindingConstants.java | 2 ++ .../internal/handler/ICalendarHandler.java | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/bundles/org.openhab.binding.icalendar/src/main/java/org/openhab/binding/icalendar/internal/ICalendarBindingConstants.java b/bundles/org.openhab.binding.icalendar/src/main/java/org/openhab/binding/icalendar/internal/ICalendarBindingConstants.java index ad1aa6428..6940c6c60 100644 --- a/bundles/org.openhab.binding.icalendar/src/main/java/org/openhab/binding/icalendar/internal/ICalendarBindingConstants.java +++ b/bundles/org.openhab.binding.icalendar/src/main/java/org/openhab/binding/icalendar/internal/ICalendarBindingConstants.java @@ -22,6 +22,7 @@ import org.openhab.core.thing.type.ChannelTypeUID; * used across the whole binding. * * @author Michael Wodniok - Initial contribution + * @author Michael Wodniok - Added last_update related constants */ @NonNullByDefault public class ICalendarBindingConstants { @@ -41,6 +42,7 @@ public class ICalendarBindingConstants { public static final String CHANNEL_NEXT_EVENT_START = "next_start"; public static final String CHANNEL_NEXT_EVENT_END = "next_end"; public static final String CHANNEL_LAST_UPDATE = "last_update"; + public static final ChannelTypeUID LAST_UPDATE_TYPE_UID = new ChannelTypeUID(BINDING_ID, "last_update"); // additional constants public static final int HTTP_TIMEOUT_SECS = 60; diff --git a/bundles/org.openhab.binding.icalendar/src/main/java/org/openhab/binding/icalendar/internal/handler/ICalendarHandler.java b/bundles/org.openhab.binding.icalendar/src/main/java/org/openhab/binding/icalendar/internal/handler/ICalendarHandler.java index 2e1c2f74b..00c9705d9 100644 --- a/bundles/org.openhab.binding.icalendar/src/main/java/org/openhab/binding/icalendar/internal/handler/ICalendarHandler.java +++ b/bundles/org.openhab.binding.icalendar/src/main/java/org/openhab/binding/icalendar/internal/handler/ICalendarHandler.java @@ -49,6 +49,8 @@ import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.binding.BaseBridgeHandler; import org.openhab.core.thing.binding.ThingHandler; +import org.openhab.core.thing.binding.builder.ChannelBuilder; +import org.openhab.core.thing.binding.builder.ThingBuilder; import org.openhab.core.types.Command; import org.openhab.core.types.RefreshType; import org.openhab.core.types.UnDefType; @@ -61,6 +63,7 @@ import org.slf4j.LoggerFactory; * * @author Michael Wodniok - Initial contribution * @author Andrew Fiddian-Green - Support for Command Tags embedded in the Event description + * @author Michael Wodniok - Added last_update-channel and additional needed handling of it */ @NonNullByDefault public class ICalendarHandler extends BaseBridgeHandler implements CalendarUpdateListener { @@ -110,6 +113,7 @@ public class ICalendarHandler extends BaseBridgeHandler implements CalendarUpdat case CHANNEL_NEXT_EVENT_TITLE: case CHANNEL_NEXT_EVENT_START: case CHANNEL_NEXT_EVENT_END: + case CHANNEL_LAST_UPDATE: if (command instanceof RefreshType) { updateStates(); } @@ -121,6 +125,8 @@ public class ICalendarHandler extends BaseBridgeHandler implements CalendarUpdat @Override public void initialize() { + migrateLastUpdateChannel(); + final ICalendarConfiguration currentConfiguration = getConfigAs(ICalendarConfiguration.class); configuration = currentConfiguration; @@ -259,6 +265,20 @@ public class ICalendarHandler extends BaseBridgeHandler implements CalendarUpdat } } + /** + * Migration for last_update-channel as this change is compatible to previous instances. + */ + private void migrateLastUpdateChannel() { + Thing thing = getThing(); + if (thing.getChannel(CHANNEL_LAST_UPDATE) == null) { + logger.debug("last_update channel is missing in this Thing. Adding it."); + ThingBuilder thingBuilder = editThing(); + ChannelBuilder channelBuilder = ChannelBuilder.create(new ChannelUID(thing.getUID(), CHANNEL_LAST_UPDATE)); + thingBuilder.withChannel(channelBuilder.withType(LAST_UPDATE_TYPE_UID).build()); + updateThing(thingBuilder.build()); + } + } + /** * Reloads the calendar from local ical-file. Replaces the class internal calendar - if loading succeeds. Else * logging details at warn-level logger.