Skip to content

Commit

Permalink
Skip fetching scene collections for Hub v1.
Browse files Browse the repository at this point in the history
Fixes openhab#11856

Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Jan 4, 2022
1 parent 40fff4d commit e6a08ab
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public class HDPowerViewBindingConstants {

public static final String BINDING_ID = "hdpowerview";

// Hub properties
public static final String PROPERTY_FIRMWARE_NAME = "firmwareName";
public static final String PROPERTY_FIRMWARE_REVISION = "firmwareRevision";
public static final String PROPERTY_FIRMWARE_SUBREVISION = "firmwareSubRevision";
public static final String PROPERTY_FIRMWARE_BUILD = "firmwareBuild";

// List of all Thing Type UIDs
public static final ThingTypeUID THING_TYPE_HUB = new ThingTypeUID(BINDING_ID, "hub");
public static final ThingTypeUID THING_TYPE_SHADE = new ThingTypeUID(BINDING_ID, "shade");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openhab.binding.hdpowerview.internal.api.ShadePosition;
import org.openhab.binding.hdpowerview.internal.api.requests.ShadeMove;
import org.openhab.binding.hdpowerview.internal.api.requests.ShadeStop;
import org.openhab.binding.hdpowerview.internal.api.responses.FirmwareVersion;
import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections;
import org.openhab.binding.hdpowerview.internal.api.responses.Scenes;
import org.openhab.binding.hdpowerview.internal.api.responses.ScheduledEvents;
Expand Down Expand Up @@ -65,6 +66,7 @@ public class HDPowerViewWebTargets {

private final String base;
private final String shades;
private final String firmwareVersion;
private final String sceneActivate;
private final String scenes;
private final String sceneCollectionActivate;
Expand Down Expand Up @@ -113,6 +115,7 @@ public String toString() {
public HDPowerViewWebTargets(HttpClient httpClient, String ipAddress) {
base = "http://" + ipAddress + "/api/";
shades = base + "shades/";
firmwareVersion = base + "fwversion/";
sceneActivate = base + "scenes";
scenes = base + "scenes/";

Expand Down Expand Up @@ -151,6 +154,20 @@ public void moveShade(int shadeId, ShadePosition position) throws HubProcessingE
invoke(HttpMethod.PUT, shades + Integer.toString(shadeId), null, json);
}

/**
* Fetches a JSON package with firmware information for the hub.
*
* @return FirmwareVersion class instance
* @throws JsonParseException if there is a JSON parsing error
* @throws HubProcessingException if there is any processing error
* @throws HubMaintenanceException if the hub is down for maintenance
*/
public @Nullable FirmwareVersion getFirmwareVersion()
throws JsonParseException, HubProcessingException, HubMaintenanceException {
String json = invoke(HttpMethod.GET, firmwareVersion, null, null);
return gson.fromJson(json, FirmwareVersion.class);
}

/**
* Fetches a JSON package that describes all scenes in the hub, and wraps it in
* a Scenes class instance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.hdpowerview.internal.api.responses;

import org.openhab.binding.hdpowerview.internal.api.responses.FirmwareVersion.Firmware.MainProcessor;

/**
* Firmware information for an HD PowerView hub
*
* @author Jacob Laursen - Initial contribution
*/
public class FirmwareVersion {
public Firmware firmware;

public static class Firmware {
public MainProcessor mainProcessor;
public Radio radio;

public static class MainProcessor {
public String name;
public int revision;
public int subRevision;
public int build;

@Override
public String toString() {
return String.format("name:%s, revision:%d, subRevision:%d, build:%d", name, revision, subRevision,
build);
}
}

public static class Radio {
public int revision;
public int subRevision;
public int build;
}
}

public MainProcessor getMainProcessor() {
if (firmware != null) {
return firmware.mainProcessor;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
import org.openhab.binding.hdpowerview.internal.HubMaintenanceException;
import org.openhab.binding.hdpowerview.internal.HubProcessingException;
import org.openhab.binding.hdpowerview.internal.api.responses.FirmwareVersion;
import org.openhab.binding.hdpowerview.internal.api.responses.FirmwareVersion.Firmware.MainProcessor;
import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections;
import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection;
import org.openhab.binding.hdpowerview.internal.api.responses.Scenes;
Expand Down Expand Up @@ -93,6 +95,7 @@ public class HDPowerViewHubHandler extends BaseBridgeHandler {
private List<Scene> sceneCache = new CopyOnWriteArrayList<>();
private List<SceneCollection> sceneCollectionCache = new CopyOnWriteArrayList<>();
private List<ScheduledEvent> scheduledEventCache = new CopyOnWriteArrayList<>();
private @Nullable MainProcessor firmwareMainProcessor;
private Boolean deprecatedChannelsCreated = false;

private final ChannelTypeUID sceneChannelTypeUID = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID,
Expand Down Expand Up @@ -252,6 +255,7 @@ private synchronized void stopPoll() {
private synchronized void poll() {
try {
logger.debug("Polling for state");
fetchFirmwareVersion();
pollShades();

List<Scene> scenes = updateSceneChannels();
Expand All @@ -271,6 +275,50 @@ private synchronized void poll() {
}
}

private void fetchFirmwareVersion() throws JsonParseException, HubProcessingException, HubMaintenanceException {
if (firmwareMainProcessor != null) {
return;
}
HDPowerViewWebTargets webTargets = this.webTargets;
if (webTargets == null) {
throw new ProcessingException("Web targets not initialized");
}

FirmwareVersion firmwareVersion = webTargets.getFirmwareVersion();
if (firmwareVersion == null) {
logger.warn("Unable to get firmware version information.");
return;
}
MainProcessor mainProcessor = firmwareVersion.getMainProcessor();
if (mainProcessor == null) {
logger.warn("Invalid or incomplete firmware version information received.");
return;
}
logger.debug("Firmware version information received: {}", mainProcessor.toString());
updateFirmwareProperties(mainProcessor);
this.firmwareMainProcessor = mainProcessor;
}

private void updateFirmwareProperties(MainProcessor mainProcessor) {
Map<String, String> properties = editProperties();
if (mainProcessor.name != null) {
properties.put(HDPowerViewBindingConstants.PROPERTY_FIRMWARE_NAME, mainProcessor.name);
}
properties.put(HDPowerViewBindingConstants.PROPERTY_FIRMWARE_REVISION, String.valueOf(mainProcessor.revision));
properties.put(HDPowerViewBindingConstants.PROPERTY_FIRMWARE_SUBREVISION,
String.valueOf(mainProcessor.subRevision));
properties.put(HDPowerViewBindingConstants.PROPERTY_FIRMWARE_BUILD, String.valueOf(mainProcessor.build));
updateProperties(properties);
}

private Boolean areSceneCollectionsSupportedByHub() {
MainProcessor mainProcessor = firmwareMainProcessor;
if (mainProcessor == null) {
return false;
}
return mainProcessor.revision >= 2;
}

private void pollShades() throws JsonParseException, HubProcessingException, HubMaintenanceException {
HDPowerViewWebTargets webTargets = this.webTargets;
if (webTargets == null) {
Expand Down Expand Up @@ -405,6 +453,10 @@ private void createDeprecatedSceneChannels(List<Scene> scenes) {

private List<SceneCollection> fetchSceneCollections()
throws JsonParseException, HubProcessingException, HubMaintenanceException {
if (!areSceneCollectionsSupportedByHub()) {
return new ArrayList<SceneCollection>();
}

HDPowerViewWebTargets webTargets = this.webTargets;
if (webTargets == null) {
throw new ProcessingException("Web targets not initialized");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<property name="vendor">Hunter Douglas (Luxaflex)</property>
<property name="modelId">PowerView Hub</property>
</properties>

<representation-property>host</representation-property>

<config-description>
Expand Down

0 comments on commit e6a08ab

Please sign in to comment.