Skip to content

Commit

Permalink
feat(channel): πŸ”§ firmware channel group added
Browse files Browse the repository at this point in the history
- current firmware (read only)
- available/latest firmware (read only)

πŸ“œ minor improvements and refactoring ... (wip)

Signed-off-by: Patrik Gfeller <patrik.gfeller@proton.me>
  • Loading branch information
pgfeller committed May 3, 2024
1 parent 153d91f commit a4e7e94
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public static class ENDPOINTS {
public static final String REGISTRATIONS = "registrations";
}

public static class CHANNELS {
public static class DEVICE {
public static class INFORMATION {
public static final String FIRMWARE = "device-information#firmware";
public static final String FIRMWARE_AVAILABLE = "device-information#available-firmware";
}
}
}

public static final String APPLICATION_NAME = "openHAB";

/** Minimal API Version required. Only apiLevel >= 7 is supported. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class HueSyncDetailedDeviceInfo extends HueSyncDeviceInfo {
* User readable version of the firmware the device can upgrade to. Item is set
* to null when there is no update available.
*/
public int updatableFirmwareVersion;
public @Nullable String updatableFirmwareVersion;
/**
* 1 = regular;
* 0 = off in powersave, passthrough or sync mode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ protected boolean isRegistered() {
}

protected void setAuthentication(String token) {
if (!token.isBlank()) {
this.unsetAuthentication();
this.unsetAuthentication();

if (!token.isBlank()) {
this.authentication = new HueSyncAuthenticationResult(this.uri, token);
this.httpClient.getAuthenticationStore().addAuthenticationResult(this.authentication);
}
Expand Down Expand Up @@ -174,7 +174,7 @@ protected void dispose() {
case HttpStatus.OK_200:
return this.deserialize(response.getContentAsString(), type);
case HttpStatus.BAD_REQUEST_400:
logger.info("registration in progress: no token received yet");
logger.trace("registration in progress: no token received yet");
break;
case HttpStatus.UNAUTHORIZED_401:
logger.error("credentials missing or invalid");
Expand Down Expand Up @@ -229,9 +229,7 @@ private ContentResponse executeRequest(HttpMethod method, String endpoint, Strin
}

public void updateConfig(HueSyncConfiguration config) {
if (!config.apiAccessToken.isBlank()) {
this.registrationId = config.registrationId;
this.setAuthentication(config.apiAccessToken);
}
this.registrationId = config.registrationId;
this.setAuthentication(config.apiAccessToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
import org.openhab.binding.huesync.internal.log.HueSyncLogFactory;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.slf4j.Logger;

/**
Expand Down Expand Up @@ -83,9 +85,11 @@ public HueSyncHandler(Thing thing, HttpClientFactory httpClientFactory)
// #region private
private void stopTask(@Nullable ScheduledFuture<?> task) {
try {
if (task != null && !task.isDone()) {
task.cancel(true);
}
Optional.ofNullable(task).ifPresent((job) -> {
if (!job.isCancelled() && !job.isDone()) {
job.cancel(true);
}
});
} catch (Exception e) {
// TODO: Handle exception ...
} finally {
Expand Down Expand Up @@ -142,6 +146,15 @@ private void updateDeviceStatus(@Nullable HueSyncDetailedDeviceInfo deviceState)
this.updateStatus(ThingStatus.OFFLINE);
} else {
this.updateStatus(ThingStatus.ONLINE);

State firmwareState = new StringType(deviceState.firmwareVersion);
State firmwareAvailableState = new StringType(
Optional.ofNullable(deviceState.updatableFirmwareVersion).isPresent()
? deviceState.updatableFirmwareVersion
: deviceState.firmwareVersion);

this.updateState(HueSyncConstants.CHANNELS.DEVICE.INFORMATION.FIRMWARE, firmwareState);
this.updateState(HueSyncConstants.CHANNELS.DEVICE.INFORMATION.FIRMWARE_AVAILABLE, firmwareAvailableState);
}
}

Expand Down Expand Up @@ -196,10 +209,13 @@ private void addProperty(String key, @Nullable String value) {
// #region Override
@Override
public void initialize() {
// TODO: Check if we need to handle enable/disable state ...

updateStatus(ThingStatus.UNKNOWN);

// initialize is also called, if the thing configuration was updated
// TODO: Improve handling of configuration changes ...
this.stopTask(this.deviceRegistrationTask);
this.stopTask(this.deviceUpdateTask);

this.config = getConfigAs(HueSyncConfiguration.class);
this.connection.updateConfig(this.config);

Expand Down Expand Up @@ -234,30 +250,15 @@ public void handleCommand(ChannelUID channelUID, Command command) {
// TODO: Implementation ...
}

/*
* TODO: Solve shutdown timing problem(s):
*
* 2024-04-30 21:38:43.874 [ERROR] [sync.internal.handler.HueSyncHandler] - The service has been unregistered
* {org.openhab.core.io.net.http.TlsTrustManagerProvider}={service.id=494, service.bundleid=249,
* service.scope=singleton}
* 2024-04-30 21:38:43.875 [INFO ] [sync.internal.handler.HueSyncHandler] - Thing HueSyncBox-...
* (huesync:huesyncthing:HueSyncBox-...) disposed.
* 2024-04-30 21:38:53.085 [TRACE] [rnal.handler.tasks.HueSyncUpdateTask] - Status update query for Sync Box
* HSB1:...
* 2024-04-30 21:38:53.111 [TRACE] [sync.internal.handler.HueSyncHandler] - Current status: UNINITIALIZED
* 2024-04-30 21:38:53.113 [WARN ] [.core.thing.binding.BaseThingHandler] - Handler HueSyncHandler tried updating
* the thing status although the handler was already disposed.
*/

@Override
public void dispose() {
super.dispose();

try {
this.connection.dispose();

this.stopTask(deviceRegistrationTask);
this.stopTask(deviceUpdateTask);

this.connection.dispose();
} catch (Exception e) {
this.logger.error("{}", e.getMessage());
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ thing-type.config.huesync.thing.statusUpdateInterval.description = Seconds betwe

# channel group types

channel-group-type.huesync.device-information.label = Firmware
channel-group-type.huesync.device-information.description = Information about the installed device firmaware and available updates.

# channel types

channel-type.huesync.device-info-firmware-available.label = Latest Firmware Version
channel-type.huesync.device-info-firmware-available.description = Latest available firmware version
channel-type.huesync.device-info-firmware.label = Firmware Version
channel-type.huesync.device-info-firmware.description = Installed firmware version

# channel group types

channel-group-type.huesync.device.label = Generic device information

# channel types

channel-type.huesync.device-info.label = Last Updated
channel-type.huesync.device-info.description = The date and time when the sensor was last updated.
channel-type.huesync.device-info.state.pattern = %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS

# thing types

thing-type.huesync.huesyncthing.channel.test.label = test
thing-type.huesync.huesyncthing.channel.test.description = test

# channel group types

channel-group-type.huesync.deviceGroup.label = Device Information

# channel group types
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<thing:thing-descriptions bindingId="huesync"
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">
<!--
<xs:complexType name="itemType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="unitHint" type="xs:string" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
-->
<channel-type id="device-info-firmware">
<!--
Switch,
Rollershutter,
Contact,
String,
Number,
Dimmer,
DateTime,
Color,
Image,
Location,
Player,
Call.
-->
<item-type>String</item-type>
<label>Firmware Version</label>
<description>Installed firmware version</description>
<category>Text</category>
<state readOnly="true"/>
</channel-type>
<channel-type id="device-info-firmware-available">
<item-type>String</item-type>
<label>Latest Firmware Version</label>
<description>Latest available firmware version</description>
<category>Text</category>
<state readOnly="true"/>
</channel-type>
<!--
firmwareVersion Get string, version User readable version of the device firmware, starting with decimal major .minor .maintenance
format e.g. β€œ1.12.3”
updatableFirmwareVersion Get string, version User readable version of the firmware the device can upgrade to. Item is set
to null when there is no update available.
buildNumber Get number, uint Build number of the firmware. Unique for every build with newer builds guaranteed a higher
number
than older.
updatableBuildNumber Get number, uint Build number that is available to update to. Item is set to null when there is no
update
available.
lastCheckedUpdate Get string, time UTC time when last check for update was performed.
wifiState Get string, enum uninitialized, disconnected, lan, wan
-->
</thing:thing-descriptions>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
smart light that responds to and reflects the content you watch or listen to.
</description>

<channel-groups>
<channel-group id="device-information" typeId="device-information"></channel-group>
</channel-groups>

<properties>
<property name="vendor">Philips</property>
Expand All @@ -20,11 +23,15 @@
<representation-property>host</representation-property>

<config-description-ref uri="thing-type:huesync:thing"/>

</thing-type>

<channel-group-type id="deviceGroup">
<label>Device Information</label>
<channel-group-type id="device-information">
<label>Firmware</label>
<description>Information about the installed device firmaware and available updates.</description>
<category></category>
<channels>
<channel id="firmware" typeId="device-info-firmware"></channel>
<channel id="available-firmware" typeId="device-info-firmware-available"></channel>
</channels>
</channel-group-type>

</thing:thing-descriptions>

0 comments on commit a4e7e94

Please sign in to comment.