Search in sources :

Example 6 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-addons by openhab.

the class DeviceHandler method onDeviceAdded.

@Override
public synchronized void onDeviceAdded(GeneralDeviceInformation device) {
    if (device instanceof Device) {
        this.device = (Device) device;
        if (this.device.isPresent()) {
            ThingStatusInfo statusInfo = this.dssBridgeHandler.getThing().getStatusInfo();
            updateStatus(statusInfo.getStatus(), statusInfo.getStatusDetail(), statusInfo.getDescription());
            logger.debug("Set status to {}", getThing().getStatus());
            // load scene configurations persistently into the thing
            for (Short i : this.device.getSavedScenes()) {
                onSceneConfigAdded(i);
            }
            logger.debug("Load saved scene specification into device");
            this.device.saveConfigSceneSpecificationIntoDevice(getThing().getProperties());
            checkDeviceInfoProperties(this.device);
            // load sensor priorities into the device and load sensor channels of the thing
            if (!this.device.isShade()) {
                loadSensorChannels();
                // check and load output channel of the thing
                checkOutputChannel();
            } else if (this.device.isBlind()) {
                // load channel for set the angle of jalousie devices
                ApplicationGroup.Color color = ((Device) device).getFunctionalColorGroup() != null ? ((Device) device).getFunctionalColorGroup().getColor() : null;
                String channelTypeID = DsChannelTypeProvider.getOutputChannelTypeID(color, ((Device) device).getOutputMode(), ((Device) device).getOutputChannels());
                loadOutputChannel(new ChannelTypeUID(BINDING_ID, channelTypeID), DsChannelTypeProvider.getItemType(channelTypeID));
            }
            // load first channel values
            onDeviceStateInitial(this.device);
            return;
        }
    }
    onDeviceRemoved(device);
}
Also used : ChannelTypeUID(org.openhab.core.thing.type.ChannelTypeUID) Device(org.openhab.binding.digitalstrom.internal.lib.structure.devices.Device) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo)

Example 7 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-addons by openhab.

the class LifxLightHandler method updateStatusIfChanged.

private void updateStatusIfChanged(ThingStatus status, ThingStatusDetail statusDetail) {
    ThingStatusInfo newStatusInfo = new ThingStatusInfo(status, statusDetail, null);
    Duration durationSinceLastUpdate = Duration.between(lastStatusInfoUpdate, LocalDateTime.now());
    boolean intervalElapsed = MIN_STATUS_INFO_UPDATE_INTERVAL.minus(durationSinceLastUpdate).isNegative();
    ThingStatusInfo oldStatusInfo = statusInfo;
    if (oldStatusInfo == null || !oldStatusInfo.equals(newStatusInfo) || intervalElapsed) {
        statusInfo = newStatusInfo;
        lastStatusInfoUpdate = LocalDateTime.now();
        updateStatus(status, statusDetail);
    }
}
Also used : ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) Duration(java.time.Duration)

Example 8 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-addons by openhab.

the class RFXComHandlerTest method verifyStatusUpdated.

private void verifyStatusUpdated(ThingStatus status, ThingStatusDetail thingStatusDetail) {
    verify(callback).statusUpdated(eq(thing), thingStatusInfoCaptor.capture());
    ThingStatusInfo tsi = thingStatusInfoCaptor.getValue();
    assertEquals(status, tsi.getStatus());
    assertEquals(thingStatusDetail, tsi.getStatusDetail());
}
Also used : ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo)

Example 9 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-addons by openhab.

the class ModbusDataHandlerTest method createTcpMock.

private Bridge createTcpMock() {
    Bridge tcpBridge = ModbusPollerThingHandlerTest.createTcpThingBuilder("tcp1").build();
    ModbusTcpThingHandler tcpThingHandler = Mockito.mock(ModbusTcpThingHandler.class);
    tcpBridge.setStatusInfo(new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, ""));
    tcpBridge.setHandler(tcpThingHandler);
    doReturn(comms).when(tcpThingHandler).getCommunicationInterface();
    try {
        doReturn(0).when(tcpThingHandler).getSlaveId();
    } catch (EndpointNotInitializedException e) {
        // not raised -- we are mocking return value only, not actually calling the method
        throw new IllegalStateException();
    }
    tcpThingHandler.initialize();
    assertThat(tcpBridge.getStatus(), is(equalTo(ThingStatus.ONLINE)));
    return tcpBridge;
}
Also used : ModbusTcpThingHandler(org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) EndpointNotInitializedException(org.openhab.binding.modbus.handler.EndpointNotInitializedException) Bridge(org.openhab.core.thing.Bridge)

Example 10 with ThingStatusInfo

use of org.openhab.core.thing.ThingStatusInfo in project openhab-addons by openhab.

the class TeslaAccountHandler method authenticate.

private ThingStatusInfo authenticate() {
    TokenResponse token = logonToken;
    boolean hasExpired = true;
    if (token != null) {
        Instant tokenCreationInstant = Instant.ofEpochMilli(token.created_at * 1000);
        logger.debug("Found a request token created at {}", dateFormatter.format(tokenCreationInstant));
        Instant tokenExpiresInstant = Instant.ofEpochMilli(token.created_at * 1000 + 60 * token.expires_in);
        if (tokenExpiresInstant.isBefore(Instant.now())) {
            logger.debug("The token has expired at {}", dateFormatter.format(tokenExpiresInstant));
            hasExpired = true;
        } else {
            hasExpired = false;
        }
    }
    if (hasExpired) {
        String username = (String) getConfig().get(CONFIG_USERNAME);
        String password = (String) getConfig().get(CONFIG_PASSWORD);
        String refreshToken = (String) getConfig().get(CONFIG_REFRESHTOKEN);
        if (refreshToken == null || refreshToken.isEmpty()) {
            if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
                try {
                    refreshToken = ssoHandler.authenticate(username, password);
                } catch (Exception e) {
                    logger.error("An exception occurred while obtaining refresh token with username/password: '{}'", e.getMessage());
                }
                if (refreshToken != null) {
                    // store refresh token from SSO endpoint in config, clear the password
                    Configuration cfg = editConfiguration();
                    cfg.put(TeslaBindingConstants.CONFIG_REFRESHTOKEN, refreshToken);
                    cfg.remove(TeslaBindingConstants.CONFIG_PASSWORD);
                    updateConfiguration(cfg);
                } else {
                    return new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Failed to obtain refresh token with username/password.");
                }
            } else {
                return new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Neither a refresh token nor credentials are provided.");
            }
        }
        this.logonToken = ssoHandler.getAccessToken(refreshToken);
        if (this.logonToken == null) {
            return new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Failed to obtain access token for API.");
        }
    }
    return new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
}
Also used : TokenResponse(org.openhab.binding.tesla.internal.protocol.sso.TokenResponse) Configuration(org.openhab.core.config.core.Configuration) Instant(java.time.Instant) ThingStatusInfo(org.openhab.core.thing.ThingStatusInfo) IOException(java.io.IOException)

Aggregations

ThingStatusInfo (org.openhab.core.thing.ThingStatusInfo)101 Test (org.junit.jupiter.api.Test)58 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)33 Thing (org.openhab.core.thing.Thing)22 ThingHandler (org.openhab.core.thing.binding.ThingHandler)22 ThingHandlerCallback (org.openhab.core.thing.binding.ThingHandlerCallback)20 ThingTypeUID (org.openhab.core.thing.ThingTypeUID)17 ThingHandlerFactory (org.openhab.core.thing.binding.ThingHandlerFactory)15 Nullable (org.eclipse.jdt.annotation.Nullable)14 Configuration (org.openhab.core.config.core.Configuration)13 ThingUID (org.openhab.core.thing.ThingUID)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)12 Bridge (org.openhab.core.thing.Bridge)12 JavaTest (org.openhab.core.test.java.JavaTest)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 DiscoveryResult (org.openhab.core.config.discovery.DiscoveryResult)5 BaseThingHandler (org.openhab.core.thing.binding.BaseThingHandler)5 ArrayList (java.util.ArrayList)4 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)4