From 65218f81fc7dc93673e0df5215b1869199f732c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20K=C3=BCper?= Date: Sat, 6 Feb 2021 16:37:29 +0100 Subject: [PATCH] Added rule condition for not a holiday (#2043) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sönke Küper --- .../EphemerisModuleHandlerFactory.java | 5 ++-- .../handler/EphemerisConditionHandler.java | 3 ++ .../moduletypes/EphemerisConditions.json | 16 ++++++++++ .../EphemerisModuleHandlerFactoryTest.java | 30 +++++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/factory/EphemerisModuleHandlerFactory.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/factory/EphemerisModuleHandlerFactory.java index 997dcb5edf5..ca28b151b1e 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/factory/EphemerisModuleHandlerFactory.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/factory/EphemerisModuleHandlerFactory.java @@ -44,8 +44,8 @@ public class EphemerisModuleHandlerFactory extends BaseModuleHandlerFactory impl private final Logger logger = LoggerFactory.getLogger(EphemerisModuleHandlerFactory.class); private static final Collection TYPES = List.of(EphemerisConditionHandler.HOLIDAY_MODULE_TYPE_ID, - EphemerisConditionHandler.WEEKEND_MODULE_TYPE_ID, EphemerisConditionHandler.WEEKDAY_MODULE_TYPE_ID, - EphemerisConditionHandler.DAYSET_MODULE_TYPE_ID); + EphemerisConditionHandler.NOT_HOLIDAY_MODULE_TYPE_ID, EphemerisConditionHandler.WEEKEND_MODULE_TYPE_ID, + EphemerisConditionHandler.WEEKDAY_MODULE_TYPE_ID, EphemerisConditionHandler.DAYSET_MODULE_TYPE_ID); private final EphemerisManager ephemerisManager; @@ -73,6 +73,7 @@ public Collection getTypes() { if (module instanceof Condition) { switch (moduleTypeUID) { case EphemerisConditionHandler.HOLIDAY_MODULE_TYPE_ID: + case EphemerisConditionHandler.NOT_HOLIDAY_MODULE_TYPE_ID: case EphemerisConditionHandler.WEEKEND_MODULE_TYPE_ID: case EphemerisConditionHandler.WEEKDAY_MODULE_TYPE_ID: case EphemerisConditionHandler.DAYSET_MODULE_TYPE_ID: diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/EphemerisConditionHandler.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/EphemerisConditionHandler.java index 554d5d1a691..bd498e7d657 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/EphemerisConditionHandler.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/EphemerisConditionHandler.java @@ -32,6 +32,7 @@ public class EphemerisConditionHandler extends BaseModuleHandler implements ConditionHandler { public static final String HOLIDAY_MODULE_TYPE_ID = "ephemeris.HolidayCondition"; + public static final String NOT_HOLIDAY_MODULE_TYPE_ID = "ephemeris.NotHolidayCondition"; public static final String WEEKEND_MODULE_TYPE_ID = "ephemeris.WeekendCondition"; public static final String WEEKDAY_MODULE_TYPE_ID = "ephemeris.WeekdayCondition"; public static final String DAYSET_MODULE_TYPE_ID = "ephemeris.DaysetCondition"; @@ -65,6 +66,8 @@ public boolean isSatisfied(Map inputs) { switch (module.getTypeUID()) { case HOLIDAY_MODULE_TYPE_ID: return ephemerisManager.isBankHoliday(target); + case NOT_HOLIDAY_MODULE_TYPE_ID: + return !ephemerisManager.isBankHoliday(target); case WEEKEND_MODULE_TYPE_ID: return ephemerisManager.isWeekend(target); case WEEKDAY_MODULE_TYPE_ID: diff --git a/bundles/org.openhab.core.automation/src/main/resources/OH-INF/automation/moduletypes/EphemerisConditions.json b/bundles/org.openhab.core.automation/src/main/resources/OH-INF/automation/moduletypes/EphemerisConditions.json index 1a2ad48a447..56f08e7253a 100644 --- a/bundles/org.openhab.core.automation/src/main/resources/OH-INF/automation/moduletypes/EphemerisConditions.json +++ b/bundles/org.openhab.core.automation/src/main/resources/OH-INF/automation/moduletypes/EphemerisConditions.json @@ -16,6 +16,22 @@ } ] }, + { + "uid": "ephemeris.NotHolidayCondition", + "label": "it is not a holiday", + "description": "Checks if the current day is not a holiday.", + "configDescriptions": [ + { + "name": "offset", + "type": "INTEGER", + "label": "Offset", + "description": "Today +/- offset days (+1 = tomorrow, -1 = yesterday).", + "default": 0, + "stepsize": 1, + "required": false + } + ] + }, { "uid": "ephemeris.WeekendCondition", "label": "it is a weekend", diff --git a/bundles/org.openhab.core.automation/src/test/java/org/openhab/core/automation/internal/module/factory/EphemerisModuleHandlerFactoryTest.java b/bundles/org.openhab.core.automation/src/test/java/org/openhab/core/automation/internal/module/factory/EphemerisModuleHandlerFactoryTest.java index f1272aa547e..2ab25b65c19 100644 --- a/bundles/org.openhab.core.automation/src/test/java/org/openhab/core/automation/internal/module/factory/EphemerisModuleHandlerFactoryTest.java +++ b/bundles/org.openhab.core.automation/src/test/java/org/openhab/core/automation/internal/module/factory/EphemerisModuleHandlerFactoryTest.java @@ -80,4 +80,34 @@ public void testFactoryCreatesModuleHandlerForWeekdayCondition() { assertThat(handler, is(notNullValue())); assertThat(handler, instanceOf(EphemerisConditionHandler.class)); } + + @Test + public void testFactoryCreatesModuleHandlerForHolidayCondition() { + when(moduleMock.getTypeUID()).thenReturn(EphemerisConditionHandler.HOLIDAY_MODULE_TYPE_ID); + + when(moduleMock.getConfiguration()).thenReturn(new Configuration()); + ModuleHandler handler = factory.internalCreate(moduleMock, "My first rule"); + assertThat(handler, is(notNullValue())); + assertThat(handler, instanceOf(EphemerisConditionHandler.class)); + + when(moduleMock.getConfiguration()).thenReturn(new Configuration(Map.of("offset", 5))); + handler = factory.internalCreate(moduleMock, "My second rule"); + assertThat(handler, is(notNullValue())); + assertThat(handler, instanceOf(EphemerisConditionHandler.class)); + } + + @Test + public void testFactoryCreatesModuleHandlerForNotHolidayCondition() { + when(moduleMock.getTypeUID()).thenReturn(EphemerisConditionHandler.NOT_HOLIDAY_MODULE_TYPE_ID); + + when(moduleMock.getConfiguration()).thenReturn(new Configuration()); + ModuleHandler handler = factory.internalCreate(moduleMock, "My first rule"); + assertThat(handler, is(notNullValue())); + assertThat(handler, instanceOf(EphemerisConditionHandler.class)); + + when(moduleMock.getConfiguration()).thenReturn(new Configuration(Map.of("offset", 5))); + handler = factory.internalCreate(moduleMock, "My second rule"); + assertThat(handler, is(notNullValue())); + assertThat(handler, instanceOf(EphemerisConditionHandler.class)); + } }