-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[nikohomecontrol] Add electricity meters and car chargers #19477
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
66ab135
car chargers
mherwege 97ca57f
grid power measurements
mherwege 6baa00a
implement energy metering in NHCII
mherwege bd45032
add gas and water metering to NHCII
mherwege 028cff6
ssl context
mherwege 2e18a19
comment
mherwege d58931d
add header
mherwege 4c8b5df
fix charger fields not poplulated
mherwege 4355287
fix channel labels
mherwege 02c97ab
fix car charger power
mherwege fdb3ad4
copilot review corrections
mherwege a1efd78
review adjustments
mherwege 7615e0a
update README
mherwege File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...ontrol/src/main/java/org/openhab/binding/nikohomecontrol/internal/SslContextProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright (c) 2010-2025 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.nikohomecontrol.internal; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.KeyManagementException; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.CertificateFactory; | ||
import java.security.cert.X509Certificate; | ||
import java.util.ResourceBundle; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.TrustManager; | ||
import javax.net.ssl.TrustManagerFactory; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.eclipse.jetty.util.ssl.SslContextFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Utility to provide a shared SSLContext and Jetty SslContextFactory | ||
* that trusts the built-in Niko Home Control controller certificates. | ||
* | ||
* @author Mark Herwege - Initial Contribution | ||
*/ | ||
@NonNullByDefault | ||
public final class SslContextProvider { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SslContextProvider.class); | ||
|
||
private @Nullable static SSLContext sslContext; | ||
private @Nullable static TrustManager @Nullable [] trustManagers = null; | ||
|
||
private SslContextProvider() { | ||
} | ||
|
||
public static synchronized SSLContext getSSLContext() throws CertificateException { | ||
SSLContext context = sslContext; | ||
if (context == null) { | ||
context = buildSSLContext(); | ||
} | ||
sslContext = context; | ||
return context; | ||
} | ||
|
||
public static synchronized TrustManager[] getTrustManagers() throws CertificateException { | ||
TrustManager[] managers = trustManagers; | ||
if (managers == null) { | ||
managers = importCertificates(); | ||
} | ||
trustManagers = managers; | ||
return managers; | ||
} | ||
|
||
public static synchronized SslContextFactory.Client getSslContextFactory() throws CertificateException { | ||
SslContextFactory.Client factory = new SslContextFactory.Client(); | ||
factory.setSslContext(getSSLContext()); | ||
factory.setEndpointIdentificationAlgorithm(null); // disable hostname verification (allows IP to be used) | ||
return factory; | ||
} | ||
|
||
private static SSLContext buildSSLContext() throws CertificateException { | ||
SSLContext context; | ||
try { | ||
context = SSLContext.getInstance("TLS"); | ||
context.init(null, getTrustManagers(), null); | ||
} catch (NoSuchAlgorithmException | KeyManagementException e) { | ||
LOGGER.debug("error with SSL context creation: {}", e.getMessage()); | ||
throw new CertificateException("SSL context creation exception", e); | ||
} | ||
|
||
LOGGER.debug("Initialized SSLContext with embedded Niko Home Control certificates"); | ||
return context; | ||
} | ||
|
||
private static TrustManager[] importCertificates() throws CertificateException { | ||
ResourceBundle certificatesBundle = ResourceBundle.getBundle("nikohomecontrol/certificates"); | ||
|
||
try { | ||
// Load server public certificates into key store | ||
CertificateFactory cf = CertificateFactory.getInstance("X509"); | ||
InputStream certificateStream; | ||
final KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
keyStore.load(null, null); | ||
for (String certName : certificatesBundle.keySet()) { | ||
certificateStream = new ByteArrayInputStream( | ||
certificatesBundle.getString(certName).getBytes(StandardCharsets.UTF_8)); | ||
X509Certificate certificate = (X509Certificate) cf.generateCertificate(certificateStream); | ||
keyStore.setCertificateEntry(certName, certificate); | ||
} | ||
|
||
ResourceBundle.clearCache(); | ||
|
||
// Create trust managers used to validate server | ||
TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
tmFactory.init(keyStore); | ||
return tmFactory.getTrustManagers(); | ||
} catch (CertificateException | KeyStoreException | NoSuchAlgorithmException | IOException e) { | ||
LOGGER.debug("error with SSL context creation: {} ", e.getMessage()); | ||
throw new CertificateException("SSL context creation exception", e); | ||
} finally { | ||
ResourceBundle.clearCache(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.