use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class NeeoDeviceHandler method initializeTask.
/**
* Initializes the task be creating the {@link NeeoDeviceProtocol}, going online and then scheduling the refresh
* task.
*/
private void initializeTask() {
final NeeoDeviceConfig config = getConfigAs(NeeoDeviceConfig.class);
final String roomKey = getRoomKey();
if (roomKey == null || roomKey.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Room key (from the parent room bridge) was not found");
return;
}
final String deviceKey = config.getDeviceKey();
if (deviceKey == null || deviceKey.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Device key was not found or empty");
return;
}
try {
NeeoUtil.checkInterrupt();
final NeeoBrainApi brainApi = getNeeoBrainApi();
if (brainApi == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "Cannot find the NEEO Brain API");
return;
}
final NeeoRoom room = brainApi.getRoom(roomKey);
final NeeoDevice device = room.getDevices().getDevice(deviceKey);
if (device == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Device (" + config.getDeviceKey() + ") was not found in room (" + roomKey + ")");
return;
}
final ThingUID thingUid = getThing().getUID();
final Map<String, String> properties = new HashMap<>();
final NeeoDeviceDetails details = device.getDetails();
if (details != null) {
/**
* The following properties have matches in org.openhab.io.neeo.OpenHabToDeviceConverter.java
*/
addProperty(properties, "Source Name", details.getSourceName());
addProperty(properties, "Adapter Name", details.getAdapterName());
addProperty(properties, "Type", details.getType());
addProperty(properties, "Manufacturer", details.getManufacturer());
addProperty(properties, "Name", details.getName());
final NeeoDeviceDetailsTiming timing = details.getTiming();
if (timing != null) {
properties.put("Standby Command Delay", toString(timing.getStandbyCommandDelay()));
properties.put("Source Switch Delay", toString(timing.getSourceSwitchDelay()));
properties.put("Shutdown Delay", toString(timing.getShutdownDelay()));
}
properties.put("Device Capabilities", Arrays.stream(details.getDeviceCapabilities()).collect(Collectors.joining(",")));
}
final ThingBuilder thingBuilder = editThing();
thingBuilder.withLabel(device.getName() + " (NEEO " + brainApi.getBrain().getKey() + ")").withProperties(properties).withChannels(ChannelUtils.generateChannels(thingUid, device));
updateThing(thingBuilder.build());
NeeoUtil.checkInterrupt();
final NeeoDeviceProtocol protocol = new NeeoDeviceProtocol(new NeeoHandlerCallback() {
@Override
public void statusChanged(ThingStatus status, ThingStatusDetail detail, String msg) {
updateStatus(status, detail, msg);
}
@Override
public void stateChanged(String channelId, State state) {
updateState(channelId, state);
}
@Override
public void setProperty(String propertyName, String propertyValue) {
getThing().setProperty(propertyName, propertyValue);
}
@Override
public void scheduleTask(Runnable task, long milliSeconds) {
scheduler.schedule(task, milliSeconds, TimeUnit.MILLISECONDS);
}
@Override
public void triggerEvent(String channelID, String event) {
triggerChannel(channelID, event);
}
@Nullable
@Override
public NeeoBrainApi getApi() {
return getNeeoBrainApi();
}
}, roomKey, deviceKey);
deviceProtocol.getAndSet(protocol);
NeeoUtil.checkInterrupt();
updateStatus(ThingStatus.ONLINE);
} catch (IOException e) {
logger.debug("IOException during initialization", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Room " + roomKey + " couldn't be found");
} catch (InterruptedException e) {
logger.debug("Initialization was interrupted", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, "Initialization was interrupted");
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class NeeoRoomHandler method initializeTask.
/**
* Initializes the task be creating the {@link NeeoRoomProtocol}, going online and then scheduling the refresh task.
*/
private void initializeTask() {
final NeeoRoomConfig config = getConfigAs(NeeoRoomConfig.class);
final String roomKey = config.getRoomKey();
if (roomKey == null || roomKey.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Room key (from the parent room bridge) was not found");
return;
}
try {
NeeoUtil.checkInterrupt();
final NeeoBrainApi brainApi = getNeeoBrainApi();
if (brainApi == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "Cannot find the NEEO Brain API");
return;
}
final NeeoRoom room = brainApi.getRoom(roomKey);
final ThingUID thingUid = getThing().getUID();
final Map<String, String> properties = new HashMap<>();
properties.put("Key", roomKey);
final ThingBuilder thingBuilder = editThing();
thingBuilder.withLabel(room.getName() + " (NEEO " + brainApi.getBrain().getKey() + ")").withProperties(properties).withChannels(ChannelUtils.generateChannels(thingUid, room));
updateThing(thingBuilder.build());
NeeoUtil.checkInterrupt();
final NeeoRoomProtocol protocol = new NeeoRoomProtocol(new NeeoHandlerCallback() {
@Override
public void statusChanged(ThingStatus status, ThingStatusDetail detail, String msg) {
updateStatus(status, detail, msg);
}
@Override
public void stateChanged(String channelId, State state) {
updateState(channelId, state);
}
@Override
public void setProperty(String propertyName, String propertyValue) {
getThing().setProperty(propertyName, propertyValue);
}
@Override
public void scheduleTask(Runnable task, long milliSeconds) {
scheduler.schedule(task, milliSeconds, TimeUnit.MILLISECONDS);
}
@Override
public void triggerEvent(String channelID, String event) {
triggerChannel(channelID, event);
}
@Nullable
@Override
public NeeoBrainApi getApi() {
return getNeeoBrainApi();
}
}, roomKey);
roomProtocol.getAndSet(protocol);
NeeoUtil.checkInterrupt();
updateStatus(ThingStatus.ONLINE);
if (config.getRefreshPolling() > 0) {
NeeoUtil.checkInterrupt();
NeeoUtil.cancel(refreshTask.getAndSet(scheduler.scheduleWithFixedDelay(() -> {
try {
refreshState();
} catch (InterruptedException e) {
logger.debug("Refresh State was interrupted", e);
}
}, 0, config.getRefreshPolling(), TimeUnit.SECONDS)));
}
} catch (IOException e) {
logger.debug("IOException during initialization", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Room " + config.getRoomKey() + " couldn't be found");
} catch (InterruptedException e) {
logger.debug("Initialization was interrupted", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, "Initialization was interrupted");
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class OpenWeatherMapAirPollutionHandler method initialize.
@Override
public void initialize() {
super.initialize();
logger.debug("Initialize OpenWeatherMapAirPollutionHandler handler '{}'.", getThing().getUID());
OpenWeatherMapAirPollutionConfiguration config = getConfigAs(OpenWeatherMapAirPollutionConfiguration.class);
boolean configValid = true;
int newForecastHours = config.forecastHours;
if (newForecastHours < 0 || newForecastHours > 120) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/offline.conf-error-not-supported-air-pollution-number-of-hours");
configValid = false;
}
if (configValid) {
logger.debug("Rebuilding thing '{}'.", getThing().getUID());
List<Channel> toBeAddedChannels = new ArrayList<>();
List<Channel> toBeRemovedChannels = new ArrayList<>();
if (forecastHours != newForecastHours) {
logger.debug("Rebuilding air pollution channel groups.");
if (forecastHours > newForecastHours) {
for (int i = newForecastHours + 1; i <= forecastHours; i++) {
toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_HOURLY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i)));
}
} else {
for (int i = forecastHours + 1; i <= newForecastHours; i++) {
toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_HOURLY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i), CHANNEL_GROUP_TYPE_AIR_POLLUTION_FORECAST));
}
}
forecastHours = newForecastHours;
}
ThingBuilder builder = editThing().withoutChannels(toBeRemovedChannels);
for (Channel channel : toBeAddedChannels) {
builder.withChannel(channel);
}
updateThing(builder.build());
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class OpenWeatherMapOneCallHandler method initialize.
@Override
public void initialize() {
super.initialize();
logger.debug("Initialize OpenWeatherMapOneCallHandler handler '{}'.", getThing().getUID());
OpenWeatherMapOneCallConfiguration config = getConfigAs(OpenWeatherMapOneCallConfiguration.class);
boolean configValid = true;
int newForecastMinutes = config.forecastMinutes;
if (newForecastMinutes < 0 || newForecastMinutes > 60) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/offline.conf-error-not-supported-onecall-number-of-minutes");
configValid = false;
}
int newForecastHours = config.forecastHours;
if (newForecastHours < 0 || newForecastHours > 48) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/offline.conf-error-not-supported-onecall-number-of-hours");
configValid = false;
}
int newForecastDays = config.forecastDays;
if (newForecastDays < 0 || newForecastDays > 8) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/offline.conf-error-not-supported-onecall-number-of-days");
configValid = false;
}
int newNumberOfAlerts = config.numberOfAlerts;
if (newNumberOfAlerts < 0) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/offline.conf-error-not-supported-onecall-number-of-alerts");
configValid = false;
}
if (configValid) {
logger.debug("Rebuilding thing '{}'.", getThing().getUID());
List<Channel> toBeAddedChannels = new ArrayList<>();
List<Channel> toBeRemovedChannels = new ArrayList<>();
toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_ONECALL_CURRENT, CHANNEL_GROUP_TYPE_ONECALL_CURRENT));
if (forecastMinutes != newForecastMinutes) {
logger.debug("forecastMinutes changed from {} to {}. Rebuilding minutely forecast channel groups.", forecastMinutes, newForecastMinutes);
if (forecastMinutes > newForecastMinutes) {
for (int i = newForecastMinutes + 1; i <= forecastMinutes; i++) {
toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i)));
}
} else {
for (int i = forecastMinutes + 1; i <= newForecastMinutes; i++) {
toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i), CHANNEL_GROUP_TYPE_ONECALL_MINUTELY_FORECAST));
}
}
forecastMinutes = newForecastMinutes;
}
if (forecastHours != newForecastHours) {
logger.debug("ForecastHours changed from {} to {}. Rebuilding hourly forecast channel groups.", forecastHours, newForecastHours);
if (forecastHours > newForecastHours) {
for (int i = newForecastHours + 1; i <= forecastHours; i++) {
toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_HOURLY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i)));
}
} else {
for (int i = forecastHours + 1; i <= newForecastHours; i++) {
toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_HOURLY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i), CHANNEL_GROUP_TYPE_ONECALL_HOURLY_FORECAST));
}
}
forecastHours = newForecastHours;
}
if (forecastDays != newForecastDays) {
logger.debug("ForecastDays changed from {} to {}. Rebuilding daily forecast channel groups.", forecastDays, newForecastDays);
if (forecastDays > newForecastDays) {
if (newForecastDays < 1) {
toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_FORECAST_TODAY));
}
if (newForecastDays < 2) {
toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_FORECAST_TOMORROW));
}
for (int i = newForecastDays; i < forecastDays; ++i) {
toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_DAILY_FORECAST_PREFIX + Integer.toString(i)));
}
} else {
if (forecastDays == 0 && newForecastDays > 0) {
toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_FORECAST_TODAY, CHANNEL_GROUP_TYPE_ONECALL_DAILY_FORECAST));
}
if (forecastDays <= 1 && newForecastDays > 1) {
toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_FORECAST_TOMORROW, CHANNEL_GROUP_TYPE_ONECALL_DAILY_FORECAST));
}
for (int i = Math.max(forecastDays, 2); i < newForecastDays; ++i) {
toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_DAILY_FORECAST_PREFIX + Integer.toString(i), CHANNEL_GROUP_TYPE_ONECALL_DAILY_FORECAST));
}
}
forecastDays = newForecastDays;
if (numberOfAlerts != newNumberOfAlerts) {
logger.debug("Rebuilding alerts channel groups.");
if (numberOfAlerts > newNumberOfAlerts) {
for (int i = newNumberOfAlerts + 1; i <= numberOfAlerts; ++i) {
toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_ALERTS_PREFIX + Integer.toString(i)));
}
} else {
for (int i = numberOfAlerts + 1; i <= newNumberOfAlerts; ++i) {
toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_ALERTS_PREFIX + Integer.toString(i), CHANNEL_GROUP_TYPE_ONECALL_ALERTS));
}
}
numberOfAlerts = newNumberOfAlerts;
}
}
logger.debug("toBeRemovedChannels: {}. toBeAddedChannels: {}", toBeRemovedChannels, toBeAddedChannels);
ThingBuilder builder = editThing().withoutChannels(toBeRemovedChannels);
for (Channel channel : toBeAddedChannels) {
builder.withChannel(channel);
}
updateThing(builder.build());
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class OpenSprinklerDeviceHandler method initialize.
@Override
public void initialize() {
super.initialize();
OpenSprinklerApi localAPI = getApi();
// Remove channels due to missing sensors or old firmware
if (localAPI != null) {
ArrayList<Channel> removeChannels = new ArrayList<>();
Channel channel = thing.getChannel(SENSOR_CURRENT_DRAW);
if (localAPI.currentDraw() == -1 && channel != null) {
logger.debug("No current sensor detected, removing channel.");
removeChannels.add(channel);
}
channel = thing.getChannel(SENSOR_SIGNAL_STRENGTH);
if (localAPI.signalStrength() == 1 && channel != null) {
removeChannels.add(channel);
}
channel = thing.getChannel(SENSOR_FLOW_COUNT);
if (localAPI.flowSensorCount() == -1 && channel != null) {
removeChannels.add(channel);
}
channel = thing.getChannel(SENSOR_2);
if (localAPI.getSensor2State() == -1 && channel != null) {
removeChannels.add(channel);
}
if (!removeChannels.isEmpty()) {
ThingBuilder thingBuilder = editThing();
thingBuilder.withoutChannels(removeChannels);
updateThing(thingBuilder.build());
}
updateProgramsChanOptions(localAPI);
updateStationsChanOptions(localAPI);
nextDurationTime = new BigDecimal(1800);
updateState(NEXT_DURATION, new QuantityType<>(nextDurationTime, Units.SECOND));
}
}
Aggregations