Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[netatmo] Thing properties were no more provided #16530

Merged
merged 7 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public enum MeasureClass {

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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public boolean isReachable() {
return dashboardData;
}

public @Nullable String getFirmware() {
return firmware;
public Optional<String> getFirmware() {
return Optional.ofNullable(firmware);
}

public int getRadioStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -104,32 +101,18 @@ 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;
}

protected void afterNewData(@Nullable NAObject newData) {
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) {
Expand Down Expand Up @@ -169,11 +152,9 @@ public void initialize() {
}

public void expireData() {
if (!handler.getCapabilities().containsKey(RefreshCapability.class)) {
CommonInterface bridgeHandler = handler.getBridgeHandler();
if (bridgeHandler != null) {
bridgeHandler.expireData();
}
CommonInterface bridgeHandler = handler.getBridgeHandler();
if (bridgeHandler != null && !handler.getCapabilities().containsKey(RefreshCapability.class)) {
bridgeHandler.expireData();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,9 +44,17 @@ public ChannelHelperCapability(CommonInterface handler, List<ChannelHelper> chan
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();
Expand All @@ -57,6 +69,15 @@ public void afterNewData(@Nullable NAObject newData) {
}
}
});
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -66,6 +66,12 @@ public void initialize() {
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
Expand All @@ -76,12 +82,6 @@ public void dispose() {

@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));
Expand All @@ -93,13 +93,13 @@ protected void updateHomeData(HomeData home) {

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ public AirQualityChannelHelper(Set<String> providedGroups) {

@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;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ public void setNewData(@Nullable NAObject data) {

@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;
};
}
}