[myStrom] Request info is not supported by the first generation of plug (#11854)
* Request info is not supported by the first generation of plug closes #10432 Signed-off-by: Frederic Chastagnol <fchastagnol@fredoware.ch>pull/11877/head
parent
b6af3aba9f
commit
96a8f942b1
|
@ -46,8 +46,11 @@ import org.openhab.core.thing.Thing;
|
||||||
import org.openhab.core.thing.ThingStatus;
|
import org.openhab.core.thing.ThingStatus;
|
||||||
import org.openhab.core.thing.ThingStatusDetail;
|
import org.openhab.core.thing.ThingStatusDetail;
|
||||||
import org.openhab.core.thing.binding.BaseThingHandler;
|
import org.openhab.core.thing.binding.BaseThingHandler;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonSyntaxException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link AbstractMyStromHandler} is responsible for handling commands, which are
|
* The {@link AbstractMyStromHandler} is responsible for handling commands, which are
|
||||||
|
@ -64,6 +67,7 @@ public abstract class AbstractMyStromHandler extends BaseThingHandler {
|
||||||
protected String hostname = "";
|
protected String hostname = "";
|
||||||
protected String mac = "";
|
protected String mac = "";
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(AbstractMyStromHandler.class);
|
||||||
private @Nullable ScheduledFuture<?> pollingJob;
|
private @Nullable ScheduledFuture<?> pollingJob;
|
||||||
protected final Gson gson = new Gson();
|
protected final Gson gson = new Gson();
|
||||||
|
|
||||||
|
@ -91,7 +95,8 @@ public abstract class AbstractMyStromHandler extends BaseThingHandler {
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateProperties() throws MyStromException {
|
private void updateProperties() {
|
||||||
|
try {
|
||||||
String json = sendHttpRequest(HttpMethod.GET, "/api/v1/info", null);
|
String json = sendHttpRequest(HttpMethod.GET, "/api/v1/info", null);
|
||||||
MyStromDeviceInfo deviceInfo = gson.fromJson(json, MyStromDeviceInfo.class);
|
MyStromDeviceInfo deviceInfo = gson.fromJson(json, MyStromDeviceInfo.class);
|
||||||
if (deviceInfo == null) {
|
if (deviceInfo == null) {
|
||||||
|
@ -110,9 +115,13 @@ public abstract class AbstractMyStromHandler extends BaseThingHandler {
|
||||||
properties.put(PROPERTY_STATIC, Boolean.toString(deviceInfo.staticState));
|
properties.put(PROPERTY_STATIC, Boolean.toString(deviceInfo.staticState));
|
||||||
properties.put(PROPERTY_CONNECTED, Boolean.toString(deviceInfo.connected));
|
properties.put(PROPERTY_CONNECTED, Boolean.toString(deviceInfo.connected));
|
||||||
Calendar calendar = Calendar.getInstance();
|
Calendar calendar = Calendar.getInstance();
|
||||||
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.getDefault());
|
DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM,
|
||||||
|
Locale.getDefault());
|
||||||
properties.put(PROPERTY_LAST_REFRESH, formatter.format(calendar.getTime()));
|
properties.put(PROPERTY_LAST_REFRESH, formatter.format(calendar.getTime()));
|
||||||
updateProperties(properties);
|
updateProperties(properties);
|
||||||
|
} catch (JsonSyntaxException | MyStromException ex) {
|
||||||
|
logger.debug("Updating properties failed: ", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -147,6 +156,7 @@ public abstract class AbstractMyStromHandler extends BaseThingHandler {
|
||||||
private void initializeInternal() {
|
private void initializeInternal() {
|
||||||
try {
|
try {
|
||||||
updateProperties();
|
updateProperties();
|
||||||
|
checkRequiredInfo();
|
||||||
updateStatus(ThingStatus.ONLINE);
|
updateStatus(ThingStatus.ONLINE);
|
||||||
MyStromConfiguration config = getConfigAs(MyStromConfiguration.class);
|
MyStromConfiguration config = getConfigAs(MyStromConfiguration.class);
|
||||||
pollingJob = scheduler.scheduleWithFixedDelay(this::pollDevice, 0, config.refresh, TimeUnit.SECONDS);
|
pollingJob = scheduler.scheduleWithFixedDelay(this::pollDevice, 0, config.refresh, TimeUnit.SECONDS);
|
||||||
|
@ -155,5 +165,8 @@ public abstract class AbstractMyStromHandler extends BaseThingHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void checkRequiredInfo() throws MyStromException {
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract void pollDevice();
|
protected abstract void pollDevice();
|
||||||
}
|
}
|
||||||
|
|
|
@ -154,6 +154,13 @@ public class MyStromBulbHandler extends AbstractMyStromHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void checkRequiredInfo() throws MyStromException {
|
||||||
|
if (mac.isBlank()) {
|
||||||
|
throw new MyStromException("Cannot retrieve MAC info from myStrom device " + getThing().getUID());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private @Nullable Map<String, MyStromDeviceSpecificInfo> getReport() {
|
private @Nullable Map<String, MyStromDeviceSpecificInfo> getReport() {
|
||||||
try {
|
try {
|
||||||
String returnContent = sendHttpRequest(HttpMethod.GET, "/api/v1/device", null);
|
String returnContent = sendHttpRequest(HttpMethod.GET, "/api/v1/device", null);
|
||||||
|
|
Loading…
Reference in New Issue