Skip to content

Commit

Permalink
[powermax] Consider TimeZoneProvider to get the timezone (openhab#7875)
Browse files Browse the repository at this point in the history
Also fix few warnings about null check

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored and andrewfg committed Aug 31, 2020
1 parent 532dcdf commit c9421c1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.config.discovery.DiscoveryService;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
Expand All @@ -48,13 +49,16 @@
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.powermax")
public class PowermaxHandlerFactory extends BaseThingHandlerFactory {

private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();

private final SerialPortManager serialPortManager;
private final TimeZoneProvider timeZoneProvider;

@Activate
public PowermaxHandlerFactory(final @Reference SerialPortManager serialPortManager) {
public PowermaxHandlerFactory(final @Reference SerialPortManager serialPortManager,
final @Reference TimeZoneProvider timeZoneProvider) {
this.serialPortManager = serialPortManager;
this.timeZoneProvider = timeZoneProvider;
}

@Override
Expand Down Expand Up @@ -89,7 +93,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
registerDiscoveryService(handler);
return handler;
} else if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
return new PowermaxThingHandler(thing);
return new PowermaxThingHandler(thing, timeZoneProvider.getTimeZone());
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingUID;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.openhab.binding.powermax.internal.PowermaxBindingConstants;
import org.openhab.binding.powermax.internal.config.PowermaxX10Configuration;
import org.openhab.binding.powermax.internal.config.PowermaxZoneConfiguration;
Expand Down Expand Up @@ -112,10 +113,10 @@ private void updateFromZoneSettings(int zoneNumber, PowermaxZoneSettings zoneSet
if (zoneSettings != null) {
// Prevent for adding already known zone
for (Thing thing : bridgeHandler.getThing().getThings()) {
ThingHandler thingHandler = thing.getHandler();
if (thing.getThingTypeUID().equals(PowermaxBindingConstants.THING_TYPE_ZONE)
&& thing.getHandler() != null) {
PowermaxZoneConfiguration config = ((PowermaxThingHandler) thing.getHandler())
.getZoneConfiguration();
&& thingHandler instanceof PowermaxThingHandler) {
PowermaxZoneConfiguration config = ((PowermaxThingHandler) thingHandler).getZoneConfiguration();
if (config.zoneNumber == zoneNumber) {
return;
}
Expand Down Expand Up @@ -147,9 +148,10 @@ private void updateFromDeviceSettings(int deviceNumber, PowermaxX10Settings devi
if (deviceSettings != null && deviceSettings.isEnabled()) {
// Prevent for adding already known X10 device
for (Thing thing : bridgeHandler.getThing().getThings()) {
ThingHandler thingHandler = thing.getHandler();
if (thing.getThingTypeUID().equals(PowermaxBindingConstants.THING_TYPE_X10)
&& thing.getHandler() != null) {
PowermaxX10Configuration config = ((PowermaxThingHandler) thing.getHandler()).getX10Configuration();
&& thingHandler instanceof PowermaxThingHandler) {
PowermaxX10Configuration config = ((PowermaxThingHandler) thingHandler).getX10Configuration();
if (config.deviceNumber == deviceNumber) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import static org.openhab.binding.powermax.internal.PowermaxBindingConstants.*;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.TimeZone;

import org.eclipse.smarthome.core.library.types.DateTimeType;
import org.eclipse.smarthome.core.library.types.OnOffType;
Expand Down Expand Up @@ -56,10 +56,13 @@ public class PowermaxThingHandler extends BaseThingHandler implements PowermaxPa
private static final int X10_NR_MIN = 1;
private static final int X10_NR_MAX = 16;

private final ZoneId zoneId;

private PowermaxBridgeHandler bridgeHandler;

public PowermaxThingHandler(Thing thing) {
public PowermaxThingHandler(Thing thing, ZoneId zoneId) {
super(thing);
this.zoneId = zoneId;
}

@Override
Expand All @@ -76,7 +79,8 @@ public void initialize() {
@Override
public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
logger.debug("Bridge status changed to {} for thing {}", bridgeStatusInfo, getThing().getUID());
initializeThingState((getBridge() == null) ? null : getBridge().getHandler(), bridgeStatusInfo.getStatus());
Bridge bridge = getBridge();
initializeThingState((bridge == null) ? null : bridge.getHandler(), bridgeStatusInfo.getStatus());
}

private void initializeThingState(ThingHandler bridgeHandler, ThingStatus bridgeStatus) {
Expand Down Expand Up @@ -180,7 +184,7 @@ public void updateChannelFromAlarmState(String channel, PowermaxState state) {
updateState(TRIPPED, state.isSensorTripped(num) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
} else if (channel.equals(LAST_TRIP) && (state.getSensorLastTripped(num) != null)) {
ZonedDateTime zoned = ZonedDateTime.ofInstant(Instant.ofEpochMilli(state.getSensorLastTripped(num)),
TimeZone.getDefault().toZoneId());
zoneId);
updateState(LAST_TRIP, new DateTimeType(zoned));
} else if (channel.equals(BYPASSED) && (state.isSensorBypassed(num) != null)) {
updateState(BYPASSED, state.isSensorBypassed(num) ? OnOffType.ON : OnOffType.OFF);
Expand Down

0 comments on commit c9421c1

Please sign in to comment.