Skip to content

Commit

Permalink
Adapt to core changes (ThingHandlerService) (openhab#16107)
Browse files Browse the repository at this point in the history
Related to: openhab/openhab-core#3957

Signed-off-by: Jan N. Klug <github@klug.nrw>
  • Loading branch information
J-N-K authored and Cybso committed Jan 5, 2024
1 parent 23bf51c commit a55e2d8
Show file tree
Hide file tree
Showing 188 changed files with 2,303 additions and 4,166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {

return THING_TYPE_STATION.equals(thingTypeUID)
? new AirQualityStationHandler(thing, timeZoneProvider, locationProvider)
: BRIDGE_TYPE_API.equals(thingTypeUID) ? new AirQualityBridgeHandler((Bridge) thing, locationProvider)
: null;
: BRIDGE_TYPE_API.equals(thingTypeUID) ? new AirQualityBridgeHandler((Bridge) thing) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.airquality.internal.handler.AirQualityBridgeHandler;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.i18n.LocationProvider;
import org.openhab.core.library.types.PointType;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ServiceScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -38,40 +38,28 @@
*
* @author Gaël L'hopital - Initial Contribution
*/
@Component(service = DiscoveryService.class, configurationPid = "discovery.airquality")
@Component(scope = ServiceScope.PROTOTYPE, service = AirQualityDiscoveryService.class, configurationPid = "discovery.airquality")
@NonNullByDefault
public class AirQualityDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
public class AirQualityDiscoveryService extends AbstractThingHandlerDiscoveryService<AirQualityBridgeHandler>
implements ThingHandlerService {
private static final int DISCOVER_TIMEOUT_SECONDS = 2;
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_STATION);

private final Logger logger = LoggerFactory.getLogger(AirQualityDiscoveryService.class);

private @Nullable LocationProvider locationProvider;
private @Nullable AirQualityBridgeHandler bridgeHandler;
private @NonNullByDefault({}) LocationProvider locationProvider;

/**
* Creates an AirQualityDiscoveryService with enabled autostart.
*/
@Activate
public AirQualityDiscoveryService() {
super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS, false);
super(AirQualityBridgeHandler.class, SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS, false);
}

@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof AirQualityBridgeHandler bridgeHandlerInstance) {
this.bridgeHandler = bridgeHandlerInstance;
this.locationProvider = bridgeHandler.getLocationProvider();
}
}

@Override
public @Nullable ThingHandler getThingHandler() {
return bridgeHandler;
}

@Override
public void deactivate() {
super.deactivate();
@Reference(unbind = "-")
public void bindLocationProvider(LocationProvider locationProvider) {
this.locationProvider = locationProvider;
}

@Override
Expand All @@ -80,7 +68,7 @@ protected void startScan() {
LocationProvider provider = locationProvider;
if (provider != null) {
PointType location = provider.getLocation();
AirQualityBridgeHandler bridge = this.bridgeHandler;
AirQualityBridgeHandler bridge = this.thingHandler;
if (location == null || bridge == null) {
logger.info("openHAB server location is not defined, will not provide any discovery results");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.airquality.internal.api.ApiBridge;
import org.openhab.binding.airquality.internal.discovery.AirQualityDiscoveryService;
import org.openhab.core.i18n.LocationProvider;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.ThingStatus;
Expand All @@ -36,12 +35,10 @@
*/
@NonNullByDefault
public class AirQualityBridgeHandler extends BaseBridgeHandler {
private final LocationProvider locationProvider;
private @Nullable ApiBridge apiBridge;

public AirQualityBridgeHandler(Bridge bridge, LocationProvider locationProvider) {
public AirQualityBridgeHandler(Bridge bridge) {
super(bridge);
this.locationProvider = locationProvider;
}

@Override
Expand Down Expand Up @@ -69,8 +66,4 @@ public void handleCommand(ChannelUID channelUID, Command command) {
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Set.of(AirQualityDiscoveryService.class);
}

public LocationProvider getLocationProvider() {
return locationProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.thing.binding.ThingActionsScope;
import org.openhab.core.thing.binding.ThingHandler;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,6 +30,7 @@
*
* @author Bob Adair - Initial contribution
*/
@Component(scope = ServiceScope.PROTOTYPE, service = BridgeActions.class)
@ThingActionsScope(name = "alarmdecoder")
@NonNullByDefault
public class BridgeActions implements ThingActions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateOption;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;

/**
* This class provides the list of valid inputs for the input channel of a source.
*
* @author Kai Kreuzer - Initial contribution
*
*/
@Component(scope = ServiceScope.PROTOTYPE, service = { DynamicStateDescriptionProvider.class,
InputStateOptionProvider.class })
@NonNullByDefault
public class InputStateOptionProvider extends BaseDynamicStateDescriptionProvider implements ThingHandlerService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.thing.type.DynamicCommandDescriptionProvider;
import org.openhab.core.types.CommandDescription;
import org.openhab.core.types.CommandOption;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;

/**
* This class provides the list of valid commands for the preset channel.
*
* @author Kai Kreuzer - Initial contribution
*
*/
@Component(scope = ServiceScope.PROTOTYPE, service = { PresetCommandOptionProvider.class,
DynamicCommandDescriptionProvider.class })
@NonNullByDefault
public class PresetCommandOptionProvider extends BaseDynamicCommandDescriptionProvider implements ThingHandlerService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.openhab.core.library.types.PercentType;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,6 +41,7 @@
* @author Kai Kreuzer - Initial contribution
*
*/
@Component(scope = ServiceScope.PROTOTYPE, service = PAAudioSink.class)
@NonNullByDefault
public class PAAudioSink extends AudioSinkSync implements ThingHandlerService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,45 @@
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.amplipi.internal.AmpliPiBindingConstants;
import org.openhab.binding.amplipi.internal.AmpliPiHandler;
import org.openhab.binding.amplipi.internal.AmpliPiStatusChangeListener;
import org.openhab.binding.amplipi.internal.model.Group;
import org.openhab.binding.amplipi.internal.model.Status;
import org.openhab.binding.amplipi.internal.model.Zone;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;

/**
* This class discoveres the available zones and groups of the AmpliPi system.
*
* @author Kai Kreuzer - Initial contribution
*
*/
@Component(scope = ServiceScope.PROTOTYPE, service = AmpliPiZoneAndGroupDiscoveryService.class)
@NonNullByDefault
public class AmpliPiZoneAndGroupDiscoveryService extends AbstractDiscoveryService
implements ThingHandlerService, AmpliPiStatusChangeListener {
public class AmpliPiZoneAndGroupDiscoveryService extends AbstractThingHandlerDiscoveryService<AmpliPiHandler>
implements AmpliPiStatusChangeListener {

private static final int TIMEOUT = 10;

private @Nullable AmpliPiHandler handler;
private List<Zone> zones = List.of();
private List<Group> groups = List.of();

public AmpliPiZoneAndGroupDiscoveryService() throws IllegalArgumentException {
super(Set.of(AmpliPiBindingConstants.THING_TYPE_GROUP, AmpliPiBindingConstants.THING_TYPE_ZONE), TIMEOUT, true);
super(AmpliPiHandler.class,
Set.of(AmpliPiBindingConstants.THING_TYPE_GROUP, AmpliPiBindingConstants.THING_TYPE_ZONE), TIMEOUT,
true);
}

@Override
public void setThingHandler(ThingHandler handler) {
AmpliPiHandler ampliPiHander = (AmpliPiHandler) handler;
ampliPiHander.addStatusChangeListener(this);
this.handler = ampliPiHander;
}

@Override
public @Nullable ThingHandler getThingHandler() {
return handler;
public void initialize() {
thingHandler.addStatusChangeListener(this);
super.initialize();
}

@Override
Expand All @@ -75,33 +70,27 @@ protected void startScan() {
}

private void createZone(Zone z) {
if (handler != null) {
ThingUID bridgeUID = handler.getThing().getUID();
ThingUID uid = new ThingUID(AmpliPiBindingConstants.THING_TYPE_ZONE, bridgeUID, z.getId().toString());
DiscoveryResult result = DiscoveryResultBuilder.create(uid).withLabel("AmpliPi Zone '" + z.getName() + "'")
.withProperty(AmpliPiBindingConstants.CFG_PARAM_ID, z.getId()).withBridge(bridgeUID)
.withRepresentationProperty(AmpliPiBindingConstants.CFG_PARAM_ID).build();
thingDiscovered(result);
}
ThingUID bridgeUID = thingHandler.getThing().getUID();
ThingUID uid = new ThingUID(AmpliPiBindingConstants.THING_TYPE_ZONE, bridgeUID, z.getId().toString());
DiscoveryResult result = DiscoveryResultBuilder.create(uid).withLabel("AmpliPi Zone '" + z.getName() + "'")
.withProperty(AmpliPiBindingConstants.CFG_PARAM_ID, z.getId()).withBridge(bridgeUID)
.withRepresentationProperty(AmpliPiBindingConstants.CFG_PARAM_ID).build();
thingDiscovered(result);
}

private void createGroup(Group g) {
if (handler != null) {
ThingUID bridgeUID = handler.getThing().getUID();
ThingUID uid = new ThingUID(AmpliPiBindingConstants.THING_TYPE_GROUP, bridgeUID, g.getId().toString());
DiscoveryResult result = DiscoveryResultBuilder.create(uid).withLabel("AmpliPi Group '" + g.getName() + "'")
.withProperty(AmpliPiBindingConstants.CFG_PARAM_ID, g.getId()).withBridge(bridgeUID)
.withRepresentationProperty(AmpliPiBindingConstants.CFG_PARAM_ID).build();
thingDiscovered(result);
}
ThingUID bridgeUID = thingHandler.getThing().getUID();
ThingUID uid = new ThingUID(AmpliPiBindingConstants.THING_TYPE_GROUP, bridgeUID, g.getId().toString());
DiscoveryResult result = DiscoveryResultBuilder.create(uid).withLabel("AmpliPi Group '" + g.getName() + "'")
.withProperty(AmpliPiBindingConstants.CFG_PARAM_ID, g.getId()).withBridge(bridgeUID)
.withRepresentationProperty(AmpliPiBindingConstants.CFG_PARAM_ID).build();
thingDiscovered(result);
}

@Override
public void deactivate() {
if (handler != null) {
handler.removeStatusChangeListener(this);
}
super.deactivate();
public void dispose() {
super.dispose();
thingHandler.removeStatusChangeListener(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.thing.binding.ThingActionsScope;
import org.openhab.core.thing.binding.ThingHandler;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,6 +41,7 @@
*
* @author Gaël L'hopital - Initial contribution
*/
@Component(scope = ServiceScope.PROTOTYPE, service = AstroActions.class)
@ThingActionsScope(name = "astro")
@NonNullByDefault
public class AstroActions implements ThingActions {
Expand Down
Loading

0 comments on commit a55e2d8

Please sign in to comment.