I took it upon me to learn Java and binding development and I made some progress. A first test version of a binding to get data from a Philips Air Purifier is getting along quite nice; the Diffie-Hellman key exchange, the Thing creation, the device status query into a Java object all work fine.
However, as a beginner on this topic, I somehow overlook an important piece of information. For some reason, the channels are not created. The relevant pieces of code are below (I am now testing with just one channel).
thing-type.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<thing:thing-descriptions bindingId="philipsair"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:thing="https://openhab.org/schemas/thing-description/v1.0.0"
xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd">
<!-- Philips Air Purifier Thing Type -->
<thing-type id="philipsair">
<label>Philips Air Purifier</label>
<description>
Provides air quality data from the Philips Air Cleaner and Humidifier.
In order to connect, the connection with the App (iOS or Android) must have been created first.
</description>
<channels>
<channel id="temperature" typeId="temperature" />
</channels>
<config-description>
<parameter name="ipAddress" type="text" required="true">
<label>IP Address</label>
<description>IP address of the Philips air cleaner and humidifier</description>
</parameter>
<parameter name="refresh" type="integer" min="1" required="false">
<label>Refresh interval</label>
<description>The refresh interval in minutes.</description>
<default>10</default>
</parameter>
</config-description>
</thing-type>
<channel-type id="temperature">
<item-type>Number:Temperature</item-type>
<label>Temperature</label>
<description>Current temperature</description>
<category>Temperature</category>
<state readOnly="true"/>
</channel-type>
</thing:thing-descriptions>
PhilipsJsonResponse.java
:
public class PhilipsAirJsonResponse {
@SerializedName("temp")
public BifgDecimal temperature;
public BigDecimal getTemperature() {
return temperature;
}
}
PhilipsAirHandler.java
:
private void startAutomaticRefresh() {
if (refreshJob == null || refreshJob.isCancelled()) {
Runnable runnable = () -> {
try {
airResponse = getPhilipsAirData();
for (Channel channel : getThing().getChannels()) {
logger.debug("Update channel {}", channel);
updateChannel(channel.getUID().getId(), airResponse);
}
} catch (Exception e) {
logger.error("Exception occurred during execution: {}", e.getMessage(), e);
}
logger.debug("Refresh job done");
};
PhilipsAirConfiguration config = getConfigAs(PhilipsAirConfiguration.class);
refreshJob = scheduler.scheduleWithFixedDelay(runnable, 0, config.refreshRate, TimeUnit.SECONDS);
}
}
The airResponse
object gets the required data from the JSON response, but no channels are found to update.
I looked at other bindings, but could not see a relevant difference related to this (except for bindings that created dynamic channels). I must be blind somehow, so what am I missing?