[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"),
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) {
channels.put(String.join("-", apiDescriptor, GROUP_TIMESTAMP), new MeasureChannelDetails(
GROUP_TIMESTAMP, DATETIME, "@text/extensible-channel-type.timestamp.pattern"));

View File

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

View File

@ -12,9 +12,6 @@
*/
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.HashMap;
import java.util.List;
@ -53,7 +50,7 @@ public class Capability {
protected final ModuleType moduleType;
protected final ThingUID thingUID;
protected boolean firstLaunch;
protected boolean firstLaunch = true;
protected Map<String, String> properties = Map.of();
protected @Nullable String statusReason;
@ -104,15 +101,6 @@ public class Capability {
protected void beforeNewData() {
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;
}
@ -120,16 +108,11 @@ public class Capability {
if (!properties.equals(thing.getProperties())) {
thing.setProperties(properties);
}
firstLaunch = false;
}
protected void updateNAThing(NAThing newData) {
String firmware = newData.getFirmware();
if (firmware != null && !firmware.isBlank()) {
properties.put(PROPERTY_FIRMWARE_VERSION, firmware);
}
if (!newData.isReachable()) {
statusReason = "@text/device-not-connected";
}
// do nothing by default, can be overridden by subclasses
}
protected void updateNAMain(NAMain newData) {
@ -169,13 +152,11 @@ public class Capability {
}
public void expireData() {
if (!handler.getCapabilities().containsKey(RefreshCapability.class)) {
CommonInterface bridgeHandler = handler.getBridgeHandler();
if (bridgeHandler != null) {
if (bridgeHandler != null && !handler.getCapabilities().containsKey(RefreshCapability.class)) {
bridgeHandler.expireData();
}
}
}
public void dispose() {
// do nothing by default, can be overridden by subclasses

View File

@ -12,12 +12,16 @@
*/
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 org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
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.NAThing;
import org.openhab.binding.netatmo.internal.handler.CommonInterface;
import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
import org.openhab.core.config.core.Configuration;
@ -40,9 +44,17 @@ public class ChannelHelperCapability extends Capability {
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
public void afterNewData(@Nullable NAObject newData) {
super.afterNewData(newData);
channelHelpers.forEach(helper -> helper.setNewData(newData));
handler.getActiveChannels().forEach(channel -> {
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

View File

@ -46,9 +46,9 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault
public class EnergyCapability extends RestCapability<EnergyApi> {
private final Logger logger = LoggerFactory.getLogger(EnergyCapability.class);
private final NetatmoDescriptionProvider descriptionProvider;
private int setPointDefaultDuration = -1;
private final NetatmoDescriptionProvider descriptionProvider;
private String energyId = "";
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 Set<FeatureArea> featureAreas = new HashSet<>();
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) {
super(handler, HomeApi.class);
@ -66,6 +66,12 @@ public class HomeCapability extends RestCapability<HomeApi> {
if (!config.securityId.isBlank()) {
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
@ -76,12 +82,6 @@ public class HomeCapability extends RestCapability<HomeApi> {
@Override
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) {
home.getCountry().map(country -> properties.put(PROPERTY_COUNTRY, country));
home.getTimezone().map(tz -> properties.put(PROPERTY_TIMEZONE, tz));
@ -93,13 +93,13 @@ public class HomeCapability extends RestCapability<HomeApi> {
@Override
protected void afterNewData(@Nullable NAObject newData) {
super.afterNewData(newData);
if (firstLaunch && !hasArea(FeatureArea.SECURITY)) {
handler.removeChannels(thing.getChannelsOfGroup(GROUP_SECURITY));
}
if (firstLaunch && !hasArea(FeatureArea.ENERGY)) {
handler.removeChannels(thing.getChannelsOfGroup(GROUP_ENERGY));
}
super.afterNewData(newData);
}
private boolean hasArea(FeatureArea searched) {

View File

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

View File

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