[netatmo] Thing properties were no more provided (#16530)

Signed-off-by: clinique <gael@lhopital.org>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Gaël L'hopital 2024-03-17 21:00:29 +01:00 committed by Ciprian Pascu
parent 13940df9bd
commit 2deacd47e1
8 changed files with 52 additions and 54 deletions

View File

@ -105,7 +105,7 @@ public class NetatmoConstants {
channels.put(String.join("-", apiDescriptor, "measurement"), channels.put(String.join("-", apiDescriptor, "measurement"),
new MeasureChannelDetails(confFragment, String.join(":", NUMBER, dimension), new MeasureChannelDetails(confFragment, String.join(":", NUMBER, dimension),
String.format("%%.%df %s", measureDefinition.scale, UnitUtils.UNIT_PLACEHOLDER))); "%%.%df %s".formatted(measureDefinition.scale, UnitUtils.UNIT_PLACEHOLDER)));
if (canScale) { if (canScale) {
channels.put(String.join("-", apiDescriptor, GROUP_TIMESTAMP), new MeasureChannelDetails( channels.put(String.join("-", apiDescriptor, GROUP_TIMESTAMP), new MeasureChannelDetails(
GROUP_TIMESTAMP, DATETIME, "@text/extensible-channel-type.timestamp.pattern")); GROUP_TIMESTAMP, DATETIME, "@text/extensible-channel-type.timestamp.pattern"));

View File

@ -59,8 +59,8 @@ public class NAThing extends NAObject implements NAModule {
return dashboardData; return dashboardData;
} }
public @Nullable String getFirmware() { public Optional<String> getFirmware() {
return firmware; return Optional.ofNullable(firmware);
} }
public int getRadioStatus() { public int getRadioStatus() {

View File

@ -12,9 +12,6 @@
*/ */
package org.openhab.binding.netatmo.internal.handler.capability; package org.openhab.binding.netatmo.internal.handler.capability;
import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
import static org.openhab.core.thing.Thing.*;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -53,7 +50,7 @@ public class Capability {
protected final ModuleType moduleType; protected final ModuleType moduleType;
protected final ThingUID thingUID; protected final ThingUID thingUID;
protected boolean firstLaunch; protected boolean firstLaunch = true;
protected Map<String, String> properties = Map.of(); protected Map<String, String> properties = Map.of();
protected @Nullable String statusReason; protected @Nullable String statusReason;
@ -104,15 +101,6 @@ public class Capability {
protected void beforeNewData() { protected void beforeNewData() {
properties = new HashMap<>(thing.getProperties()); properties = new HashMap<>(thing.getProperties());
firstLaunch = properties.isEmpty();
if (firstLaunch) {
properties.put(PROPERTY_THING_TYPE_VERSION, moduleType.thingTypeVersion);
if (!moduleType.isLogical()) {
String name = moduleType.apiName.isBlank() ? moduleType.name() : moduleType.apiName;
properties.put(PROPERTY_MODEL_ID, name);
properties.put(PROPERTY_VENDOR, VENDOR);
}
}
statusReason = null; statusReason = null;
} }
@ -120,16 +108,11 @@ public class Capability {
if (!properties.equals(thing.getProperties())) { if (!properties.equals(thing.getProperties())) {
thing.setProperties(properties); thing.setProperties(properties);
} }
firstLaunch = false;
} }
protected void updateNAThing(NAThing newData) { protected void updateNAThing(NAThing newData) {
String firmware = newData.getFirmware(); // do nothing by default, can be overridden by subclasses
if (firmware != null && !firmware.isBlank()) {
properties.put(PROPERTY_FIRMWARE_VERSION, firmware);
}
if (!newData.isReachable()) {
statusReason = "@text/device-not-connected";
}
} }
protected void updateNAMain(NAMain newData) { protected void updateNAMain(NAMain newData) {
@ -169,13 +152,11 @@ public class Capability {
} }
public void expireData() { public void expireData() {
if (!handler.getCapabilities().containsKey(RefreshCapability.class)) {
CommonInterface bridgeHandler = handler.getBridgeHandler(); CommonInterface bridgeHandler = handler.getBridgeHandler();
if (bridgeHandler != null) { if (bridgeHandler != null && !handler.getCapabilities().containsKey(RefreshCapability.class)) {
bridgeHandler.expireData(); bridgeHandler.expireData();
} }
} }
}
public void dispose() { public void dispose() {
// do nothing by default, can be overridden by subclasses // do nothing by default, can be overridden by subclasses

View File

@ -12,12 +12,16 @@
*/ */
package org.openhab.binding.netatmo.internal.handler.capability; package org.openhab.binding.netatmo.internal.handler.capability;
import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.VENDOR;
import static org.openhab.core.thing.Thing.*;
import java.util.List; import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.netatmo.internal.api.dto.NAError; import org.openhab.binding.netatmo.internal.api.dto.NAError;
import org.openhab.binding.netatmo.internal.api.dto.NAObject; import org.openhab.binding.netatmo.internal.api.dto.NAObject;
import org.openhab.binding.netatmo.internal.api.dto.NAThing;
import org.openhab.binding.netatmo.internal.handler.CommonInterface; import org.openhab.binding.netatmo.internal.handler.CommonInterface;
import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper; import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
import org.openhab.core.config.core.Configuration; import org.openhab.core.config.core.Configuration;
@ -40,9 +44,17 @@ public class ChannelHelperCapability extends Capability {
this.channelHelpers = channelHelpers; this.channelHelpers = channelHelpers;
} }
@Override
protected void beforeNewData() {
super.beforeNewData();
if (firstLaunch && !moduleType.isLogical()) {
properties.put(PROPERTY_MODEL_ID, moduleType.apiName.isBlank() ? moduleType.name() : moduleType.apiName);
properties.put(PROPERTY_VENDOR, VENDOR);
}
}
@Override @Override
public void afterNewData(@Nullable NAObject newData) { public void afterNewData(@Nullable NAObject newData) {
super.afterNewData(newData);
channelHelpers.forEach(helper -> helper.setNewData(newData)); channelHelpers.forEach(helper -> helper.setNewData(newData));
handler.getActiveChannels().forEach(channel -> { handler.getActiveChannels().forEach(channel -> {
ChannelUID channelUID = channel.getUID(); ChannelUID channelUID = channel.getUID();
@ -57,6 +69,15 @@ public class ChannelHelperCapability extends Capability {
} }
} }
}); });
super.afterNewData(newData);
}
@Override
protected void updateNAThing(NAThing newData) {
newData.getFirmware().map(fw -> properties.put(PROPERTY_FIRMWARE_VERSION, fw));
if (!newData.isReachable()) {
statusReason = "@text/device-not-connected";
}
} }
@Override @Override

View File

@ -46,9 +46,9 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault @NonNullByDefault
public class EnergyCapability extends RestCapability<EnergyApi> { public class EnergyCapability extends RestCapability<EnergyApi> {
private final Logger logger = LoggerFactory.getLogger(EnergyCapability.class); private final Logger logger = LoggerFactory.getLogger(EnergyCapability.class);
private final NetatmoDescriptionProvider descriptionProvider;
private int setPointDefaultDuration = -1; private int setPointDefaultDuration = -1;
private final NetatmoDescriptionProvider descriptionProvider;
private String energyId = ""; private String energyId = "";
EnergyCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider) { EnergyCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider) {

View File

@ -48,7 +48,7 @@ public class HomeCapability extends RestCapability<HomeApi> {
private final Logger logger = LoggerFactory.getLogger(HomeCapability.class); private final Logger logger = LoggerFactory.getLogger(HomeCapability.class);
private final Set<FeatureArea> featureAreas = new HashSet<>(); private final Set<FeatureArea> featureAreas = new HashSet<>();
private final NetatmoDescriptionProvider descriptionProvider; private final NetatmoDescriptionProvider descriptionProvider;
private final Set<String> homeIds = new HashSet<>(); private final Set<String> homeIds = new HashSet<>(3);
public HomeCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider) { public HomeCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider) {
super(handler, HomeApi.class); super(handler, HomeApi.class);
@ -66,6 +66,12 @@ public class HomeCapability extends RestCapability<HomeApi> {
if (!config.securityId.isBlank()) { if (!config.securityId.isBlank()) {
homeIds.add(config.securityId); homeIds.add(config.securityId);
} }
if (hasArea(FeatureArea.SECURITY) && !handler.getCapabilities().containsKey(SecurityCapability.class)) {
handler.getCapabilities().put(new SecurityCapability(handler));
}
if (hasArea(FeatureArea.ENERGY) && !handler.getCapabilities().containsKey(EnergyCapability.class)) {
handler.getCapabilities().put(new EnergyCapability(handler, descriptionProvider));
}
} }
@Override @Override
@ -76,12 +82,6 @@ public class HomeCapability extends RestCapability<HomeApi> {
@Override @Override
protected void updateHomeData(HomeData home) { protected void updateHomeData(HomeData home) {
if (hasArea(FeatureArea.SECURITY) && !handler.getCapabilities().containsKey(SecurityCapability.class)) {
handler.getCapabilities().put(new SecurityCapability(handler));
}
if (hasArea(FeatureArea.ENERGY) && !handler.getCapabilities().containsKey(EnergyCapability.class)) {
handler.getCapabilities().put(new EnergyCapability(handler, descriptionProvider));
}
if (firstLaunch) { if (firstLaunch) {
home.getCountry().map(country -> properties.put(PROPERTY_COUNTRY, country)); home.getCountry().map(country -> properties.put(PROPERTY_COUNTRY, country));
home.getTimezone().map(tz -> properties.put(PROPERTY_TIMEZONE, tz)); home.getTimezone().map(tz -> properties.put(PROPERTY_TIMEZONE, tz));
@ -93,13 +93,13 @@ public class HomeCapability extends RestCapability<HomeApi> {
@Override @Override
protected void afterNewData(@Nullable NAObject newData) { protected void afterNewData(@Nullable NAObject newData) {
super.afterNewData(newData);
if (firstLaunch && !hasArea(FeatureArea.SECURITY)) { if (firstLaunch && !hasArea(FeatureArea.SECURITY)) {
handler.removeChannels(thing.getChannelsOfGroup(GROUP_SECURITY)); handler.removeChannels(thing.getChannelsOfGroup(GROUP_SECURITY));
} }
if (firstLaunch && !hasArea(FeatureArea.ENERGY)) { if (firstLaunch && !hasArea(FeatureArea.ENERGY)) {
handler.removeChannels(thing.getChannelsOfGroup(GROUP_ENERGY)); handler.removeChannels(thing.getChannelsOfGroup(GROUP_ENERGY));
} }
super.afterNewData(newData);
} }
private boolean hasArea(FeatureArea searched) { private boolean hasArea(FeatureArea searched) {

View File

@ -39,12 +39,10 @@ public class AirQualityChannelHelper extends ChannelHelper {
@Override @Override
protected @Nullable State internalGetDashboard(String channelId, Dashboard dashboard) { protected @Nullable State internalGetDashboard(String channelId, Dashboard dashboard) {
switch (channelId) { return switch (channelId) {
case CHANNEL_CO2: case CHANNEL_CO2 -> toQuantityType(dashboard.getCo2(), MeasureClass.CO2);
return toQuantityType(dashboard.getCo2(), MeasureClass.CO2); case CHANNEL_HEALTH_INDEX -> new DecimalType(dashboard.getHealthIdx());
case CHANNEL_HEALTH_INDEX: default -> null;
return new DecimalType(dashboard.getHealthIdx()); };
}
return null;
} }
} }

View File

@ -62,14 +62,12 @@ public class SecurityChannelHelper extends ChannelHelper {
@Override @Override
protected @Nullable State internalGetOther(String channelId) { protected @Nullable State internalGetOther(String channelId) {
switch (channelId) { return switch (channelId) {
case CHANNEL_PERSON_COUNT: case CHANNEL_PERSON_COUNT -> persons != -1 ? new DecimalType(persons) : UnDefType.NULL;
return persons != -1 ? new DecimalType(persons) : UnDefType.NULL; case CHANNEL_UNKNOWN_PERSON_COUNT -> unknowns != -1 ? new DecimalType(unknowns) : UnDefType.NULL;
case CHANNEL_UNKNOWN_PERSON_COUNT: case CHANNEL_UNKNOWN_PERSON_PICTURE ->
return unknowns != -1 ? new DecimalType(unknowns) : UnDefType.NULL; unknownSnapshot != null ? toRawType(unknownSnapshot) : UnDefType.NULL;
case CHANNEL_UNKNOWN_PERSON_PICTURE: default -> null;
return unknownSnapshot != null ? toRawType(unknownSnapshot) : UnDefType.NULL; };
}
return null;
} }
} }