use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class Tr064RootHandler method internalInitialize.
/**
* internal thing initializer (sets SCPDUtil and connects to remote device)
*/
private void internalInitialize() {
try {
scpdUtil = new SCPDUtil(httpClient, endpointBaseURL);
} catch (SCPDException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "could not get device definitions from " + config.host);
return;
}
if (establishSecureConnectionAndUpdateProperties()) {
removeConnectScheduler();
// connection successful, check channels
ThingBuilder thingBuilder = editThing();
thingBuilder.withoutChannels(thing.getChannels());
final SCPDUtil scpdUtil = this.scpdUtil;
if (scpdUtil != null) {
Util.checkAvailableChannels(thing, thingBuilder, scpdUtil, "", deviceType, channels);
updateThing(thingBuilder.build());
}
communicationEstablished = true;
installPolling();
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project openhab-addons by openhab.
the class WolfSmartsetUnitThingHandler method updateConfiguration.
/**
* Update the configuration of this unit and create / update the related channels
*
* @param submenu the {@link SubMenuEntryDTO} for this unit
* @param tabmenu the {@link MenuItemTabViewDTO} for this unit
*/
public void updateConfiguration(SubMenuEntryDTO submenu, MenuItemTabViewDTO tabmenu) {
this.submenu = submenu;
this.tabmenu = tabmenu;
var bridgeHandler = getBridge();
if (bridgeHandler != null) {
bridgeStatusChanged(bridgeHandler.getStatusInfo());
}
lastRefreshTime = null;
ThingBuilder thingBuilder = editThing();
var thingId = thing.getUID();
paramDescriptionMap.clear();
for (var param : tabmenu.parameterDescriptors) {
paramDescriptionMap.put(param.valueId, param);
// "bindingId:type:thingId:1")
var channelId = new ChannelUID(thingId, param.parameterId.toString());
if (thing.getChannel(channelId) == null) {
logger.debug("UnitThing: Create channel '{}'", channelId);
Channel channel = ChannelBuilder.create(channelId, getItemType(param.controlType)).withLabel(param.name).withType(getChannelType(param)).build();
thingBuilder.withChannel(channel);
}
}
updateThing(thingBuilder.build());
for (var param : tabmenu.parameterDescriptors) {
var channelId = new ChannelUID(thingId, param.parameterId.toString());
setState(channelId, WolfSmartsetUtils.undefOrString(param.value));
}
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project addons by smarthomej.
the class LightThingHandler method processStateResponse.
@Override
protected void processStateResponse(DeconzBaseMessage stateResponse) {
if (!(stateResponse instanceof LightMessage)) {
return;
}
LightMessage lightMessage = (LightMessage) stateResponse;
if (needsPropertyUpdate) {
// if we did not receive an ctmin/ctmax, then we probably don't need it
needsPropertyUpdate = false;
Integer ctmax = lightMessage.ctmax;
Integer ctmin = lightMessage.ctmin;
if (ctmin != null && ctmax != null) {
Map<String, String> properties = new HashMap<>(thing.getProperties());
properties.put(PROPERTY_CT_MAX, Integer.toString(Util.constrainToRange(ctmax, ZCL_CT_MIN, ZCL_CT_MAX)));
properties.put(PROPERTY_CT_MIN, Integer.toString(Util.constrainToRange(ctmin, ZCL_CT_MIN, ZCL_CT_MAX)));
updateProperties(properties);
}
}
ThingBuilder thingBuilder = editThing();
boolean thingEdited = false;
LightState lightState = lightMessage.state;
if (lightState != null && lightState.effect != null && checkAndUpdateEffectChannels(thingBuilder, lightMessage)) {
thingEdited = true;
}
if (checkLastSeen(thingBuilder, stateResponse.lastseen)) {
thingEdited = true;
}
if (thingEdited) {
updateThing(thingBuilder.build());
}
messageReceived(lightMessage);
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project addons by smarthomej.
the class SensorBaseThingHandler method processStateResponse.
@Override
protected void processStateResponse(DeconzBaseMessage stateResponse) {
if (!(stateResponse instanceof SensorMessage)) {
return;
}
SensorMessage sensorMessage = (SensorMessage) stateResponse;
sensorConfig = Objects.requireNonNullElse(sensorMessage.config, new SensorConfig());
sensorState = Objects.requireNonNullElse(sensorMessage.state, new SensorState());
// Add some information about the sensor
if (!sensorConfig.reachable) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE, "Not reachable");
return;
}
Map<String, String> editProperties = editProperties();
editProperties.put(UNIQUE_ID, sensorMessage.uniqueid);
editProperties.put(Thing.PROPERTY_FIRMWARE_VERSION, sensorMessage.swversion);
editProperties.put(Thing.PROPERTY_VENDOR, sensorMessage.manufacturername);
editProperties.put(Thing.PROPERTY_MODEL_ID, sensorMessage.modelid);
ignoreConfigurationUpdate = true;
updateProperties(editProperties);
// Some sensors support optional channels
// (see https://github.com/dresden-elektronik/deconz-rest-plugin/wiki/Supported-Devices#sensors)
// any battery-powered sensor
ThingBuilder thingBuilder = editThing();
boolean thingEdited = false;
if (sensorConfig.battery != null) {
if (createChannel(thingBuilder, CHANNEL_BATTERY_LEVEL, ChannelKind.STATE)) {
thingEdited = true;
}
if (createChannel(thingBuilder, CHANNEL_BATTERY_LOW, ChannelKind.STATE)) {
thingEdited = true;
}
}
if (sensorState.lowbattery != null && sensorConfig.battery == null) {
// if sensorConfig.battery != null the channel is already added
if (createChannel(thingBuilder, CHANNEL_BATTERY_LOW, ChannelKind.STATE)) {
thingEdited = true;
}
}
if (createTypeSpecificChannels(thingBuilder, sensorConfig, sensorState)) {
thingEdited = true;
}
if (checkLastSeen(thingBuilder, sensorMessage.lastseen)) {
thingEdited = true;
}
// if the thing was edited, we update it now
if (thingEdited) {
logger.debug("Thing configuration changed, updating thing.");
updateThing(thingBuilder.build());
}
ignoreConfigurationUpdate = false;
// Initial data
updateChannels(sensorConfig);
updateChannels(sensorState, true);
updateStatus(ThingStatus.ONLINE);
}
use of org.openhab.core.thing.binding.builder.ThingBuilder in project addons by smarthomej.
the class BAE091xSensorThingHandler method configureThingChannels.
@Override
protected void configureThingChannels() {
ThingUID thingUID = getThing().getUID();
logger.debug("configuring sensors for {}", thingUID);
BAE091xHandlerConfiguration configuration = getConfig().as(BAE091xHandlerConfiguration.class);
Set<OwChannelConfig> wantedChannel = new HashSet<>();
wantedChannel.addAll(SENSOR_TYPE_CHANNEL_MAP.getOrDefault(sensorType, Set.of()));
// Pin1:
switch(configuration.pin1) {
case CONFIG_BAE_PIN_DISABLED:
break;
case CONFIG_BAE_PIN_COUNTER:
wantedChannel.add(new OwChannelConfig(CHANNEL_COUNTER, CHANNEL_TYPE_UID_BAE_COUNTER));
break;
default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 1");
return;
}
// Pin2:
switch(configuration.pin2) {
case CONFIG_BAE_PIN_DISABLED:
break;
case CONFIG_BAE_PIN_OUT:
wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL2, CHANNEL_TYPE_UID_BAE_DIGITAL_OUT, "Digital Out Pin 2"));
break;
case CONFIG_BAE_PIN_PWM:
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY3, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 3"));
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ1, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 1/3"));
break;
default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 2");
return;
}
// Pin6:
switch(configuration.pin6) {
case CONFIG_BAE_PIN_DISABLED:
break;
case CONFIG_BAE_PIN_PIO:
wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL6, CHANNEL_TYPE_UID_BAE_PIO, "PIO Pin 6"));
break;
case CONFIG_BAE_PIN_PWM:
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY4, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 4"));
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ2, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 2/4"));
break;
default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 6");
return;
}
// Pin7:
switch(configuration.pin7) {
case CONFIG_BAE_PIN_DISABLED:
break;
case CONFIG_BAE_PIN_ANALOG:
wantedChannel.add(new OwChannelConfig(CHANNEL_VOLTAGE, CHANNEL_TYPE_UID_BAE_ANALOG, "Analog Input"));
break;
case CONFIG_BAE_PIN_OUT:
wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL7, CHANNEL_TYPE_UID_BAE_DIGITAL_OUT, "Digital Out Pin 7"));
break;
case CONFIG_BAE_PIN_PWM:
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY2, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 2"));
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ2, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 2/4"));
break;
default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 7");
return;
}
// Pin8:
switch(configuration.pin8) {
case CONFIG_BAE_PIN_DISABLED:
break;
case CONFIG_BAE_PIN_IN:
wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL8, CHANNEL_TYPE_UID_BAE_DIN, "Digital In Pin 8"));
break;
case CONFIG_BAE_PIN_OUT:
wantedChannel.add(new OwChannelConfig(CHANNEL_DIGITAL8, CHANNEL_TYPE_UID_BAE_DOUT, "Digital Out Pin 8"));
break;
case CONFIG_BAE_PIN_PWM:
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_DUTY1, CHANNEL_TYPE_UID_BAE_PWM_DUTY, "Duty Cycle PWM 1"));
wantedChannel.add(new OwChannelConfig(CHANNEL_PWM_FREQ1, CHANNEL_TYPE_UID_BAE_PWM_FREQUENCY, "Frequency PWM 1/3"));
break;
default:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "unknown configuration option for pin 8");
return;
}
ThingBuilder thingBuilder = editThing();
// remove unwanted channels
Set<String> existingChannelIds = thing.getChannels().stream().map(channel -> channel.getUID().getId()).collect(Collectors.toSet());
Set<String> wantedChannelIds = wantedChannel.stream().map(channelConfig -> channelConfig.channelId).collect(Collectors.toSet());
existingChannelIds.stream().filter(channelId -> !wantedChannelIds.contains(channelId)).forEach(channelId -> removeChannelIfExisting(thingBuilder, channelId));
// add or update wanted channels
wantedChannel.forEach(channelConfig -> addChannelIfMissingAndEnable(thingBuilder, channelConfig));
updateThing(thingBuilder.build());
try {
sensors.get(0).configureChannels();
} catch (OwException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
return;
}
validConfig = true;
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE);
}
Aggregations