use of org.openhab.core.automation.Condition in project openhab-addons by openhab.
the class RuleConditionHandlerTests method ltOperator.
@Test
public void ltOperator() {
Map<String, Object> context = new TreeMap<>();
HueRuleConditionHandler subject;
Condition c;
Configuration configuration = new Configuration();
configuration.put("operator", "lt");
context.put("newState", new DecimalType(12));
context.put("oldState", new DecimalType(0));
configuration.put("value", "15");
configuration.put("address", "/groups/10/action");
c = ConditionBuilder.create().withId("a").withTypeUID(HueRuleConditionHandler.MODULE_TYPE_ID).withConfiguration(configuration).build();
subject = new HueRuleConditionHandler(c, ds);
assertThat(subject.isSatisfied(context), is(true));
}
use of org.openhab.core.automation.Condition in project openhab-addons by openhab.
the class RuleConditionHandlerTests method equalOperator.
@Test
public void equalOperator() {
Map<String, Object> context = new TreeMap<>();
HueRuleConditionHandler subject;
Condition c;
Configuration configuration = new Configuration();
configuration.put("operator", "eq");
context.put("newState", OnOffType.ON);
context.put("oldState", OnOffType.OFF);
configuration.put("value", "true");
configuration.put("address", "/lights/1/state");
c = ConditionBuilder.create().withId("a").withTypeUID(HueRuleConditionHandler.MODULE_TYPE_ID).withConfiguration(configuration).build();
subject = new HueRuleConditionHandler(c, ds);
assertThat(subject.isSatisfied(context), is(true));
context.put("newState", OpenClosedType.OPEN);
context.put("oldState", OpenClosedType.CLOSED);
configuration.put("value", "true");
configuration.put("address", "/sensors/2/state");
c = ConditionBuilder.create().withId("a").withTypeUID(HueRuleConditionHandler.MODULE_TYPE_ID).withConfiguration(configuration).build();
subject = new HueRuleConditionHandler(c, ds);
assertThat(subject.isSatisfied(context), is(true));
context.put("newState", new DecimalType(12));
context.put("oldState", new DecimalType(0));
configuration.put("value", "12");
configuration.put("address", "/groups/10/action");
c = ConditionBuilder.create().withId("a").withTypeUID(HueRuleConditionHandler.MODULE_TYPE_ID).withConfiguration(configuration).build();
subject = new HueRuleConditionHandler(c, ds);
assertThat(subject.isSatisfied(context), is(true));
}
use of org.openhab.core.automation.Condition in project openhab-core by openhab.
the class ScriptModuleHandlerFactory method internalCreate.
@Override
@Nullable
protected ModuleHandler internalCreate(Module module, String ruleUID) {
logger.trace("create {} -> {}", module.getId(), module.getTypeUID());
String moduleTypeUID = module.getTypeUID();
if (ScriptConditionHandler.TYPE_ID.equals(moduleTypeUID) && module instanceof Condition) {
ScriptConditionHandler handler = new ScriptConditionHandler((Condition) module, ruleUID, scriptEngineManager);
return handler;
} else if (ScriptActionHandler.TYPE_ID.equals(moduleTypeUID) && module instanceof Action) {
ScriptActionHandler handler = new ScriptActionHandler((Action) module, ruleUID, scriptEngineManager);
return handler;
} else {
logger.error("The ModuleHandler is not supported: {}", moduleTypeUID);
return null;
}
}
use of org.openhab.core.automation.Condition in project openhab-core by openhab.
the class ScriptRuleOSGiTest method testPredefinedRule.
// ignore - wip @Test
public void testPredefinedRule() throws ItemNotFoundException {
EventPublisher eventPublisher = getService(EventPublisher.class);
ItemRegistry itemRegistry = getService(ItemRegistry.class);
RuleRegistry ruleRegistry = getService(RuleRegistry.class);
RuleManager ruleEngine = getService(RuleManager.class);
// WAIT until Rule modules types are parsed and the rule becomes IDLE
waitForAssert(() -> {
assertThat(ruleRegistry.getAll().isEmpty(), is(false));
Rule rule2 = ruleRegistry.get("javascript.rule1");
assertThat(rule2, is(notNullValue()));
RuleStatusInfo ruleStatus2 = ruleEngine.getStatusInfo(rule2.getUID());
assertThat(ruleStatus2, is(notNullValue()));
assertThat(ruleStatus2.getStatus(), is(RuleStatus.IDLE));
}, 5000, 200);
Rule rule = ruleRegistry.get("javascript.rule1");
assertThat(rule, is(notNullValue()));
assertThat(rule.getName(), is("DemoScriptRule"));
Optional<? extends Trigger> trigger = rule.getTriggers().stream().filter(t -> "trigger".equals(t.getId())).findFirst();
assertThat(trigger.isPresent(), is(true));
assertThat(trigger.get().getTypeUID(), is("core.GenericEventTrigger"));
assertThat(trigger.get().getConfiguration().get("eventSource"), is("MyTrigger"));
assertThat(trigger.get().getConfiguration().get("eventTopic"), is("openhab/items/MyTrigger/state"));
assertThat(trigger.get().getConfiguration().get("eventTypes"), is("ItemStateEvent"));
Optional<? extends Condition> condition1 = rule.getConditions().stream().filter(c -> "condition".equals(c.getId())).findFirst();
assertThat(condition1.isPresent(), is(true));
assertThat(condition1.get().getTypeUID(), is("script.ScriptCondition"));
assertThat(condition1.get().getConfiguration().get("type"), is("application/javascript"));
assertThat(condition1.get().getConfiguration().get("script"), is("event.itemState==ON"));
Optional<? extends Action> action = rule.getActions().stream().filter(a -> "action".equals(a.getId())).findFirst();
assertThat(action.isPresent(), is(true));
assertThat(action.get().getTypeUID(), is("script.ScriptAction"));
assertThat(action.get().getConfiguration().get("type"), is("application/javascript"));
assertThat(action.get().getConfiguration().get("script"), is("print(items.MyTrigger), print(things.getAll()), print(ctx.get('trigger.event')), events.sendCommand('ScriptItem', 'ON')"));
RuleStatusInfo ruleStatus = ruleEngine.getStatusInfo(rule.getUID());
assertThat(ruleStatus.getStatus(), is(RuleStatus.IDLE));
SwitchItem myTriggerItem = (SwitchItem) itemRegistry.getItem("MyTrigger");
logger.info("Triggering item: {}", myTriggerItem.getName());
eventPublisher.post(ItemEventFactory.createStateEvent("MyTrigger", OnOffType.ON));
waitForAssert(() -> {
assertThat(receivedEvent, notNullValue());
});
assertThat(receivedEvent.getItemName(), is(equalTo("ScriptItem")));
assertThat(receivedEvent.getItemCommand(), is(OnOffType.ON));
}
use of org.openhab.core.automation.Condition in project openhab-core by openhab.
the class AutomationIntegrationTest method testModuleTypeProviderAndTemplateProvider.
@Test
public void testModuleTypeProviderAndTemplateProvider() {
logger.info("test ModuleTypeProvider and TemplateProvider");
TemplateRegistry<RuleTemplate> templateRegistry = getService(TemplateRegistry.class);
ModuleTypeRegistry moduleTypeRegistry = getService(ModuleTypeRegistry.class);
String templateUID = "testTemplate1";
Set<String> tags = Stream.of("test", "testTag").collect(toSet());
List<Trigger> templateTriggers = Collections.emptyList();
List<Condition> templateConditions = Collections.emptyList();
List<Action> templateActions = Collections.emptyList();
List<ConfigDescriptionParameter> templateConfigDescriptionParameters = List.of(ConfigDescriptionParameterBuilder.create("param", ConfigDescriptionParameter.Type.TEXT).build());
RuleTemplate template = new RuleTemplate(templateUID, "Test template Label", "Test template description", tags, templateTriggers, templateConditions, templateActions, templateConfigDescriptionParameters, Visibility.VISIBLE);
String triggerTypeUID = "testTrigger1";
TriggerType triggerType = new TriggerType(triggerTypeUID, templateConfigDescriptionParameters, null);
String actionTypeUID = "testAction1";
ActionType actionType = new ActionType(actionTypeUID, templateConfigDescriptionParameters, null);
RuleTemplateProvider templateProvider = new RuleTemplateProvider() {
@Override
@Nullable
public RuleTemplate getTemplate(String UID, @Nullable Locale locale) {
if (UID.equals(templateUID)) {
return template;
} else {
return null;
}
}
@Override
public Collection<RuleTemplate> getTemplates(@Nullable Locale locale) {
return Set.of(template);
}
@Override
public void addProviderChangeListener(ProviderChangeListener<RuleTemplate> listener) {
}
@Override
public Collection<RuleTemplate> getAll() {
return Set.of(template);
}
@Override
public void removeProviderChangeListener(ProviderChangeListener<RuleTemplate> listener) {
}
};
ModuleTypeProvider moduleTypeProvider = new ModuleTypeProvider() {
@Override
public void addProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
}
@Override
public Collection<ModuleType> getAll() {
return Stream.of(triggerType, actionType).collect(toSet());
}
@Override
public void removeProviderChangeListener(ProviderChangeListener<ModuleType> listener) {
}
@Override
@Nullable
public <T extends ModuleType> T getModuleType(String UID, @Nullable Locale locale) {
if (UID.equals(triggerTypeUID)) {
return (T) triggerType;
} else if (UID.equals(actionTypeUID)) {
return (T) actionType;
} else {
return null;
}
}
@Override
public <T extends ModuleType> Collection<T> getModuleTypes(@Nullable Locale locale) {
return (Collection<T>) Stream.of(triggerType, actionType).collect(toSet());
}
};
registerService(templateProvider);
assertThat(templateRegistry.get(templateUID), is(notNullValue()));
unregisterService(templateProvider);
assertThat(templateRegistry.get(templateUID), is(nullValue()));
registerService(moduleTypeProvider);
assertThat(moduleTypeRegistry.get(actionTypeUID), is(notNullValue()));
assertThat(moduleTypeRegistry.get(triggerTypeUID), is(notNullValue()));
unregisterService(moduleTypeProvider);
assertThat(moduleTypeRegistry.get(actionTypeUID), is(nullValue()));
assertThat(moduleTypeRegistry.get(triggerTypeUID), is(nullValue()));
}
Aggregations