Search in sources :

Example 11 with Configuration

use of org.openhab.core.config.core.Configuration in project openhab-addons by openhab.

the class HomeConnectBridgeHandler method handleConfigurationUpdate.

@Override
public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
    if (isModifyingCurrentConfig(configurationParameters)) {
        List<String> parameters = configurationParameters.entrySet().stream().map((entry) -> {
            if (CLIENT_ID.equals(entry.getKey()) || CLIENT_SECRET.equals(entry.getKey())) {
                return entry.getKey() + ": ***";
            }
            return entry.getKey() + ": " + entry.getValue();
        }).collect(Collectors.toList());
        logger.debug("Update bridge configuration. bridge={}, parameters={}", getThing().getLabel(), parameters);
        validateConfigurationParameters(configurationParameters);
        Configuration configuration = editConfiguration();
        for (Entry<String, Object> configurationParameter : configurationParameters.entrySet()) {
            configuration.put(configurationParameter.getKey(), configurationParameter.getValue());
        }
        // invalidate oAuth credentials
        try {
            logger.debug("Clear oAuth credential store. bridge={}", getThing().getLabel());
            var oAuthClientService = this.oAuthClientService;
            if (oAuthClientService != null) {
                oAuthClientService.remove();
            }
        } catch (OAuthException e) {
            logger.error("Could not clear oAuth credentials. bridge={}", getThing().getLabel(), e);
        }
        if (isInitialized()) {
            // persist new configuration and reinitialize handler
            dispose();
            updateConfiguration(configuration);
            initialize();
        } else {
            // persist new configuration and notify Thing Manager
            updateConfiguration(configuration);
            @Nullable ThingHandlerCallback callback = getCallback();
            if (callback != null) {
                callback.configurationUpdated(this.getThing());
            } else {
                logger.warn("Handler {} tried updating its configuration although the handler was already disposed.", this.getClass().getSimpleName());
            }
        }
    }
}
Also used : ApiRequest(org.openhab.binding.homeconnect.internal.client.model.ApiRequest) ScheduledFuture(java.util.concurrent.ScheduledFuture) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) ZonedDateTime(java.time.ZonedDateTime) AuthorizationException(org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException) LoggerFactory(org.slf4j.LoggerFactory) ThingHandlerService(org.openhab.core.thing.binding.ThingHandlerService) ArrayList(java.util.ArrayList) HttpClient(org.eclipse.jetty.client.HttpClient) ClientBuilder(javax.ws.rs.client.ClientBuilder) Nullable(org.eclipse.jdt.annotation.Nullable) Configuration(org.openhab.core.config.core.Configuration) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Map(java.util.Map) HomeConnectServlet(org.openhab.binding.homeconnect.internal.servlet.HomeConnectServlet) ChannelUID(org.openhab.core.thing.ChannelUID) CommunicationException(org.openhab.binding.homeconnect.internal.client.exception.CommunicationException) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) HomeConnectApiClient(org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient) ThingStatus(org.openhab.core.thing.ThingStatus) Command(org.openhab.core.types.Command) Logger(org.slf4j.Logger) OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) Collection(java.util.Collection) Event(org.openhab.binding.homeconnect.internal.client.model.Event) IOException(java.io.IOException) ThingStatusDetail(org.openhab.core.thing.ThingStatusDetail) HomeConnectEventSourceClient(org.openhab.binding.homeconnect.internal.client.HomeConnectEventSourceClient) HomeConnectBindingConstants(org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants) Collectors(java.util.stream.Collectors) SseEventSourceFactory(org.osgi.service.jaxrs.client.SseEventSourceFactory) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) ApiBridgeConfiguration(org.openhab.binding.homeconnect.internal.configuration.ApiBridgeConfiguration) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) Entry(java.util.Map.Entry) HomeConnectDiscoveryService(org.openhab.binding.homeconnect.internal.discovery.HomeConnectDiscoveryService) OAuthFactory(org.openhab.core.auth.client.oauth2.OAuthFactory) BaseBridgeHandler(org.openhab.core.thing.binding.BaseBridgeHandler) Collections(java.util.Collections) Bridge(org.openhab.core.thing.Bridge) Configuration(org.openhab.core.config.core.Configuration) ApiBridgeConfiguration(org.openhab.binding.homeconnect.internal.configuration.ApiBridgeConfiguration) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) ThingHandlerCallback(org.openhab.core.thing.binding.ThingHandlerCallback) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 12 with Configuration

use of org.openhab.core.config.core.Configuration in project openhab-addons by openhab.

the class LGWebOSHandler method findMacAddress.

/**
 * Make a best effort to automatically detect the MAC address of the TV.
 * If this does not work automatically, users can still set it manually in the Thing config.
 */
private void findMacAddress() {
    LGWebOSConfiguration c = getLGWebOSConfig();
    String host = c.getHost();
    if (!host.isEmpty()) {
        try {
            // validate host, so that no command can be injected
            String macAddress = WakeOnLanUtility.getMACAddress(InetAddress.getByName(host).getHostAddress());
            if (macAddress != null && !macAddress.equals(c.macAddress)) {
                c.macAddress = macAddress;
                // persist the configuration change
                Configuration configuration = editConfiguration();
                configuration.put(LGWebOSBindingConstants.CONFIG_MAC_ADDRESS, macAddress);
                updateConfiguration(configuration);
            }
        } catch (UnknownHostException e) {
            logger.debug("Unable to determine MAC address: {}", e.getMessage());
        }
    }
}
Also used : Configuration(org.openhab.core.config.core.Configuration) UnknownHostException(java.net.UnknownHostException)

Example 13 with Configuration

use of org.openhab.core.config.core.Configuration in project openhab-addons by openhab.

the class LGWebOSHandler method storeKey.

@Override
public void storeKey(@Nullable String key) {
    if (!getKey().equals(key)) {
        logger.debug("Store new access Key in the thing configuration");
        // store it current configuration and avoiding complete re-initialization via handleConfigurationUpdate
        getLGWebOSConfig().key = key;
        // persist the configuration change
        Configuration configuration = editConfiguration();
        configuration.put(LGWebOSBindingConstants.CONFIG_KEY, key);
        updateConfiguration(configuration);
    }
}
Also used : Configuration(org.openhab.core.config.core.Configuration)

Example 14 with Configuration

use of org.openhab.core.config.core.Configuration in project openhab-addons by openhab.

the class FSInternetRadioHandlerJavaTest method offlineIfEmptyPIN.

/**
 * Verify OFFLINE Thing status when the PIN is empty String.
 */
@Test
public void offlineIfEmptyPIN() {
    Configuration config = createConfiguration(DEFAULT_CONFIG_PROPERTY_IP, "", String.valueOf(DEFAULT_CONFIG_PROPERTY_PORT), DEFAULT_CONFIG_PROPERTY_REFRESH);
    Thing radioThingWithEmptyPIN = initializeRadioThing(config);
    testRadioThingConsideringConfiguration(radioThingWithEmptyPIN);
}
Also used : Configuration(org.openhab.core.config.core.Configuration) Thing(org.openhab.core.thing.Thing) Test(org.junit.jupiter.api.Test) JavaTest(org.openhab.core.test.java.JavaTest)

Example 15 with Configuration

use of org.openhab.core.config.core.Configuration in project openhab-addons by openhab.

the class Ipx800v3Handler method dataReceived.

@Override
public void dataReceived(String port, double value) {
    updateStatus(ThingStatus.ONLINE);
    Channel channel = thing.getChannel(PortDefinition.asChannelId(port));
    if (channel != null) {
        String channelId = channel.getUID().getId();
        String groupId = channel.getUID().getGroupId();
        PortData portData = portDatas.get(channelId);
        if (portData != null && groupId != null) {
            ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
            long sinceLastChange = Duration.between(portData.getTimestamp(), now).toMillis();
            Configuration configuration = channel.getConfiguration();
            PortDefinition portDefinition = PortDefinition.fromGroupId(groupId);
            if (ignoreCondition(value, portData, configuration, portDefinition, now)) {
                logger.debug("Ignore condition met for port '{}' with data '{}'", port, value);
                return;
            }
            logger.debug("About to update port '{}' with data '{}'", port, value);
            State state = UnDefType.UNDEF;
            switch(portDefinition) {
                case COUNTER:
                    state = new DecimalType(value);
                    break;
                case RELAY:
                    state = value == 1 ? OnOffType.ON : OnOffType.OFF;
                    break;
                case ANALOG:
                    state = new DecimalType(value);
                    updateState(channelId + PROPERTY_SEPARATOR + CHANNEL_VOLTAGE, new QuantityType<>(value * ANALOG_SAMPLING, Units.VOLT));
                    break;
                case CONTACT:
                    DigitalInputConfiguration config = configuration.as(DigitalInputConfiguration.class);
                    portData.cancelPulsing();
                    state = value == 1 ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
                    switch((OpenClosedType) state) {
                        case CLOSED:
                            if (config.longPressTime != 0 && !portData.isInitializing()) {
                                scheduler.schedule(new LongPressEvaluator(channel, port, portData), config.longPressTime, TimeUnit.MILLISECONDS);
                            } else if (config.pulsePeriod != 0) {
                                portData.setPulsing(scheduler.scheduleWithFixedDelay(() -> {
                                    triggerPushButtonChannel(channel, EVENT_PULSE);
                                }, config.pulsePeriod, config.pulsePeriod, TimeUnit.MILLISECONDS));
                                if (config.pulseTimeout != 0) {
                                    scheduler.schedule(portData::cancelPulsing, config.pulseTimeout, TimeUnit.MILLISECONDS);
                                }
                            }
                            break;
                        case OPEN:
                            if (!portData.isInitializing() && config.longPressTime != 0 && sinceLastChange < config.longPressTime) {
                                triggerPushButtonChannel(channel, EVENT_SHORT_PRESS);
                            }
                            break;
                    }
                    if (!portData.isInitializing()) {
                        triggerPushButtonChannel(channel, value == 1 ? EVENT_PRESSED : EVENT_RELEASED);
                    }
                    break;
            }
            updateState(channelId, state);
            if (!portData.isInitializing()) {
                updateState(channelId + PROPERTY_SEPARATOR + CHANNEL_LAST_STATE_DURATION, new QuantityType<>(sinceLastChange / 1000, Units.SECOND));
            }
            portData.setData(value, now);
        } else {
            logger.debug("Received data '{}' for not configured port '{}'", value, port);
        }
    } else {
        logger.debug("Received data '{}' for not configured channel '{}'", value, port);
    }
}
Also used : PortDefinition(org.openhab.binding.gce.internal.model.PortDefinition) AnalogInputConfiguration(org.openhab.binding.gce.internal.config.AnalogInputConfiguration) Configuration(org.openhab.core.config.core.Configuration) Ipx800Configuration(org.openhab.binding.gce.internal.config.Ipx800Configuration) DigitalInputConfiguration(org.openhab.binding.gce.internal.config.DigitalInputConfiguration) RelayOutputConfiguration(org.openhab.binding.gce.internal.config.RelayOutputConfiguration) Channel(org.openhab.core.thing.Channel) DigitalInputConfiguration(org.openhab.binding.gce.internal.config.DigitalInputConfiguration) ZonedDateTime(java.time.ZonedDateTime) PortData(org.openhab.binding.gce.internal.model.PortData) State(org.openhab.core.types.State) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DecimalType(org.openhab.core.library.types.DecimalType)

Aggregations

Configuration (org.openhab.core.config.core.Configuration)498 Test (org.junit.jupiter.api.Test)193 Thing (org.openhab.core.thing.Thing)97 Channel (org.openhab.core.thing.Channel)62 HashMap (java.util.HashMap)60 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)59 ChannelUID (org.openhab.core.thing.ChannelUID)50 ThingUID (org.openhab.core.thing.ThingUID)50 Bridge (org.openhab.core.thing.Bridge)49 Nullable (org.eclipse.jdt.annotation.Nullable)39 BeforeEach (org.junit.jupiter.api.BeforeEach)39 BigDecimal (java.math.BigDecimal)35 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)32 IOException (java.io.IOException)31 ArrayList (java.util.ArrayList)26 Rule (org.openhab.core.automation.Rule)24 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)24 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)22 Action (org.openhab.core.automation.Action)19 Condition (org.openhab.core.automation.Condition)19