Skip to content

Commit

Permalink
Merge branch 'openhab:main' into binding-finder
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewfg authored Nov 11, 2023
2 parents 41b4fb1 + cdbca4d commit cce11cb
Show file tree
Hide file tree
Showing 60 changed files with 2,466 additions and 516 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*/
package org.openhab.core.automation.module.script.defaultscope;

import java.time.ZonedDateTime;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.items.Item;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.TimeSeries;

/**
* The static methods of this class are made available as functions in the scripts.
Expand Down Expand Up @@ -98,14 +100,33 @@ public interface ScriptBusEvent {

/**
* Posts a status update for a specified item to the event bus.
* t
*
* @param item the item to send the status update for
* @param state the new state of the item
*/
@Nullable
Object postUpdate(@Nullable Item item, @Nullable State state);

/**
* Sends a time series to the event bus
*
* @param item the item to send the time series for
* @param timeSeries a {@link TimeSeries} containing policy and values
*/
@Nullable
Object sendTimeSeries(@Nullable Item item, @Nullable TimeSeries timeSeries);

/**
* Sends a time series to the event bus
*
* @param itemName the name of the item to send the status update for
* @param values a {@link Map} containing the timeseries, composed of pairs of {@link ZonedDateTime} and
* {@link State}
* @param policy either <code>ADD</code> or <code>REPLACE</code>
*/
@Nullable
Object sendTimeSeries(@Nullable String itemName, @Nullable Map<ZonedDateTime, State> values, String policy);

/**
* Stores the current states for a list of items in a map.
* A group item is not itself put into the map, but instead all its members.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.core.automation.module.script.internal.defaultscope;

import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -27,6 +28,7 @@
import org.openhab.core.items.events.ItemEventFactory;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.TimeSeries;
import org.openhab.core.types.TypeParser;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -153,6 +155,31 @@ public void dispose() {
return null;
}

@Override
public @Nullable Object sendTimeSeries(@Nullable Item item, @Nullable TimeSeries timeSeries) {
EventPublisher eventPublisher1 = this.eventPublisher;
if (eventPublisher1 != null && item != null && timeSeries != null) {
eventPublisher1.post(ItemEventFactory.createTimeSeriesEvent(item.getName(), timeSeries, null));
}
return null;
}

@Override
public @Nullable Object sendTimeSeries(@Nullable String itemName, @Nullable Map<ZonedDateTime, State> values,
@Nullable String policy) {
EventPublisher eventPublisher1 = this.eventPublisher;
if (eventPublisher1 != null && itemName != null && values != null && policy != null) {
try {
TimeSeries timeSeries = new TimeSeries(TimeSeries.Policy.valueOf(policy));
values.forEach((key, value) -> timeSeries.add(key.toInstant(), value));
eventPublisher1.post(ItemEventFactory.createTimeSeriesEvent(itemName, timeSeries, null));
} catch (IllegalArgumentException e) {
LoggerFactory.getLogger(ScriptBusEventImpl.class).warn("Policy '{}' does not exist.", policy);
}
}
return null;
}

@Override
public Map<Item, State> storeStates(Item @Nullable... items) {
Map<Item, State> statesMap = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ private SitemapWidgetEvent constructSitemapEventForWidget(Item item, State state
event.sitemapName = sitemapName;
event.pageId = pageId;
event.label = itemUIRegistry.getLabel(widget);
event.labelSource = itemUIRegistry.getLabelSource(widget).toString();
event.widgetId = itemUIRegistry.getWidgetId(widget);
event.icon = itemUIRegistry.getCategory(widget);
event.reloadIcon = widget.getStaticIcon() == null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ private PageDTO createPageBean(String sitemapName, @Nullable String title, @Null
bean.valuecolor = convertItemValueColor(itemUIRegistry.getValueColor(widget), itemState);
bean.iconcolor = convertItemValueColor(itemUIRegistry.getIconColor(widget), itemState);
bean.label = itemUIRegistry.getLabel(widget);
bean.labelSource = itemUIRegistry.getLabelSource(widget).toString();
bean.pattern = itemUIRegistry.getFormatPattern(widget);
bean.unit = itemUIRegistry.getUnitForWidget(widget);
bean.type = widget.eClass().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
* @author Kai Kreuzer - Initial contribution
* @author Laurent Garnier - New field iconcolor
* @author Laurent Garnier - New field reloadIcon
* @author Danny Baumann - New field labelSource
*/
public class SitemapWidgetEvent extends SitemapEvent {

public String widgetId;

public String label;
public String labelSource;
public String icon;
public boolean reloadIcon;
public String labelcolor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @author Laurent Garnier - New field iconcolor
* @author Mark herwege - New fields pattern, unit
* @author Laurent Garnier - New field columns
* @author Danny Baumann - New field labelSource
*/
public class WidgetDTO {

Expand All @@ -35,6 +36,7 @@ public class WidgetDTO {
public boolean visibility;

public String label;
public String labelSource;
public String icon;
/**
* staticIcon is a boolean indicating if the widget state must be ignored when requesting the icon.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.ui.items.ItemUIRegistry;
import org.openhab.core.ui.items.ItemUIRegistry.WidgetLabelSource;

/**
* Test aspects of the {@link SitemapResource}.
Expand All @@ -74,6 +75,7 @@ public class SitemapResourceTest extends JavaTest {

private static final String HTTP_HEADER_X_ATMOSPHERE_TRANSPORT = "X-Atmosphere-Transport";
private static final String ITEM_NAME = "itemName";
private static final String ITEM_LABEL = "item label";
private static final String SITEMAP_PATH = "/sitemaps";
private static final String SITEMAP_MODEL_NAME = "sitemapModel";
private static final String SITEMAP_NAME = "defaultSitemap";
Expand All @@ -84,7 +86,6 @@ public class SitemapResourceTest extends JavaTest {
private static final String ICON_COLOR_ITEM_NAME = "iconColorItemName";
private static final String ICON_ITEM_NAME = "iconItemName";
private static final String WIDGET1_LABEL = "widget 1";
private static final String WIDGET2_LABEL = "widget 2";
private static final String WIDGET3_LABEL = "widget 3";
private static final String WIDGET1_ID = "00";
private static final String WIDGET2_ID = "01";
Expand Down Expand Up @@ -332,6 +333,7 @@ public void whenGetPageDataShouldReturnPageBean() throws ItemNotFoundException {

assertThat(pageDTO.widgets.get(0).widgetId, is(WIDGET1_ID));
assertThat(pageDTO.widgets.get(0).label, is(WIDGET1_LABEL));
assertThat(pageDTO.widgets.get(0).labelSource, is("SITEMAP_WIDGET"));
assertThat(pageDTO.widgets.get(0).labelcolor, is("GREEN"));
assertThat(pageDTO.widgets.get(0).valuecolor, is("BLUE"));
assertThat(pageDTO.widgets.get(0).iconcolor, is("ORANGE"));
Expand All @@ -343,7 +345,8 @@ public void whenGetPageDataShouldReturnPageBean() throws ItemNotFoundException {
assertThat(pageDTO.widgets.get(0).item.state, is("50"));

assertThat(pageDTO.widgets.get(1).widgetId, is(WIDGET2_ID));
assertThat(pageDTO.widgets.get(1).label, is(WIDGET2_LABEL));
assertThat(pageDTO.widgets.get(1).label, is(ITEM_LABEL));
assertThat(pageDTO.widgets.get(1).labelSource, is("ITEM_LABEL"));
assertThat(pageDTO.widgets.get(1).labelcolor, nullValue());
assertThat(pageDTO.widgets.get(1).valuecolor, nullValue());
assertThat(pageDTO.widgets.get(1).iconcolor, nullValue());
Expand Down Expand Up @@ -379,6 +382,7 @@ private void configureItemUIRegistry(State state1, State state2) throws ItemNotF
when(itemUIRegistryMock.getWidgetId(widgets.get(0))).thenReturn(WIDGET1_ID);
when(itemUIRegistryMock.getCategory(widgets.get(0))).thenReturn(WIDGET1_ICON);
when(itemUIRegistryMock.getLabel(widgets.get(0))).thenReturn(WIDGET1_LABEL);
when(itemUIRegistryMock.getLabelSource(widgets.get(0))).thenReturn(WidgetLabelSource.SITEMAP_WIDGET);
when(itemUIRegistryMock.getVisiblity(widgets.get(0))).thenReturn(true);
when(itemUIRegistryMock.getLabelColor(widgets.get(0))).thenReturn("GREEN");
when(itemUIRegistryMock.getValueColor(widgets.get(0))).thenReturn("BLUE");
Expand All @@ -387,7 +391,8 @@ private void configureItemUIRegistry(State state1, State state2) throws ItemNotF

when(itemUIRegistryMock.getWidgetId(widgets.get(1))).thenReturn(WIDGET2_ID);
when(itemUIRegistryMock.getCategory(widgets.get(1))).thenReturn(WIDGET2_ICON);
when(itemUIRegistryMock.getLabel(widgets.get(1))).thenReturn(WIDGET2_LABEL);
when(itemUIRegistryMock.getLabel(widgets.get(1))).thenReturn(ITEM_LABEL);
when(itemUIRegistryMock.getLabelSource(widgets.get(1))).thenReturn(WidgetLabelSource.ITEM_LABEL);
when(itemUIRegistryMock.getVisiblity(widgets.get(1))).thenReturn(true);
when(itemUIRegistryMock.getLabelColor(widgets.get(1))).thenReturn(null);
when(itemUIRegistryMock.getValueColor(widgets.get(1))).thenReturn(null);
Expand All @@ -397,6 +402,7 @@ private void configureItemUIRegistry(State state1, State state2) throws ItemNotF
when(itemUIRegistryMock.getWidgetId(widgets.get(2))).thenReturn(WIDGET3_ID);
when(itemUIRegistryMock.getCategory(widgets.get(2))).thenReturn(WIDGET3_ICON);
when(itemUIRegistryMock.getLabel(widgets.get(2))).thenReturn(WIDGET3_LABEL);
when(itemUIRegistryMock.getLabelSource(widgets.get(2))).thenReturn(WidgetLabelSource.SITEMAP_WIDGET);
when(itemUIRegistryMock.getVisiblity(widgets.get(2))).thenReturn(true);
when(itemUIRegistryMock.getLabelColor(widgets.get(2))).thenReturn(null);
when(itemUIRegistryMock.getValueColor(widgets.get(2))).thenReturn(null);
Expand Down Expand Up @@ -484,7 +490,7 @@ private EList<Widget> initSitemapWidgets() {
when(switchEClass.getName()).thenReturn("switch");
when(switchEClass.getInstanceTypeName()).thenReturn("org.openhab.core.model.sitemap.Switch");
when(w2.eClass()).thenReturn(switchEClass);
when(w2.getLabel()).thenReturn(WIDGET2_LABEL);
when(w2.getLabel()).thenReturn(null);
when(w2.getItem()).thenReturn(ITEM_NAME);
when(w2.getIcon()).thenReturn(WIDGET2_ICON);
when(w2.getStaticIcon()).thenReturn(null);
Expand Down Expand Up @@ -528,6 +534,7 @@ private class TestItem extends GenericItem {

public TestItem(String name) {
super("Number", name);
label = ITEM_LABEL;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.service.StartLevelService;
import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationHelper;
import org.openhab.core.types.State;
Expand Down Expand Up @@ -62,13 +63,15 @@ public class SseItemStatesEventBuilder {
private final BundleContext bundleContext;
private final ItemRegistry itemRegistry;
private final LocaleService localeService;
private final StartLevelService startLevelService;

@Activate
public SseItemStatesEventBuilder(final BundleContext bundleContext, final @Reference ItemRegistry itemRegistry,
final @Reference LocaleService localeService) {
final @Reference LocaleService localeService, final @Reference StartLevelService startLevelService) {
this.bundleContext = bundleContext;
this.itemRegistry = itemRegistry;
this.localeService = localeService;
this.startLevelService = startLevelService;
}

public @Nullable OutboundSseEvent buildEvent(Builder eventBuilder, Set<String> itemNames) {
Expand All @@ -86,7 +89,9 @@ public SseItemStatesEventBuilder(final BundleContext bundleContext, final @Refer
}
payload.put(itemName, stateDto);
} catch (ItemNotFoundException e) {
logger.warn("Attempting to send a state update of an item which doesn't exist: {}", itemName);
if (startLevelService.getStartLevel() >= StartLevelService.STARTLEVEL_MODEL) {
logger.warn("Attempting to send a state update of an item which doesn't exist: {}", itemName);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ public String getName() {
return "restoreOnStartup";
};
};
public static final Strategy FORECAST = new StrategyImpl() {
@Override
public String getName() {
return "forecast";
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class PersistenceGlobalScopeProvider extends AbstractGlobalScopeProvider
res.getContents().add(GlobalStrategies.UPDATE);
res.getContents().add(GlobalStrategies.CHANGE);
res.getContents().add(GlobalStrategies.RESTORE);
res.getContents().add(GlobalStrategies.FORECAST);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
*/
package org.openhab.core.model.script.actions;

import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
Expand All @@ -26,6 +28,7 @@
import org.openhab.core.model.script.ScriptServiceUtil;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.TimeSeries;
import org.openhab.core.types.TypeParser;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -146,7 +149,7 @@ public static Object postUpdate(Item item, String stateAsString) {
* Posts a status update for a specified item to the event bus.
*
* @param itemName the name of the item to send the status update for
* @param stateAsString the new state of the item
* @param stateString the new state of the item
*/
public static Object postUpdate(String itemName, String stateString) {
ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
Expand All @@ -169,6 +172,43 @@ public static Object postUpdate(String itemName, String stateString) {
return null;
}

/**
* Sends a time series to the event bus
*
* @param item the item to send the time series for
* @param timeSeries a {@link TimeSeries} containing policy and values
*/
public static Object sendTimeSeries(@Nullable Item item, @Nullable TimeSeries timeSeries) {
EventPublisher eventPublisher1 = ScriptServiceUtil.getEventPublisher();
if (eventPublisher1 != null && item != null && timeSeries != null) {
eventPublisher1.post(ItemEventFactory.createTimeSeriesEvent(item.getName(), timeSeries, null));
}
return null;
}

/**
* Sends a time series to the event bus
*
* @param itemName the name of the item to send the status update for
* @param values a {@link Map} containing the timeseries, composed of pairs of {@link ZonedDateTime} and
* {@link State}
* @param policy either <code>ADD</code> or <code>REPLACE</code>
*/
public static Object sendTimeSeries(@Nullable String itemName, @Nullable Map<ZonedDateTime, State> values,
String policy) {
EventPublisher eventPublisher1 = ScriptServiceUtil.getEventPublisher();
if (eventPublisher1 != null && itemName != null && values != null && policy != null) {
try {
TimeSeries timeSeries = new TimeSeries(TimeSeries.Policy.valueOf(policy));
values.forEach((key, value) -> timeSeries.add(key.toInstant(), value));
eventPublisher1.post(ItemEventFactory.createTimeSeriesEvent(itemName, timeSeries, null));
} catch (IllegalArgumentException e) {
LoggerFactory.getLogger(BusEvent.class).warn("Policy '{}' does not exist.", policy);
}
}
return null;
}

private static <T extends State> List<String> getAcceptedDataTypeNames(Item item) {
return item.getAcceptedDataTypes().stream().map(Class::getSimpleName).toList();
}
Expand Down Expand Up @@ -232,6 +272,4 @@ public static Object restoreStates(Map<Item, State> statesMap) {
}
return null;
}

// static public JobKey timer(AbstractInstant instant, Object)
}
Loading

0 comments on commit cce11cb

Please sign in to comment.