-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[bluetooth.govee] Govee Bluetooth Binding initial contribution #8610
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9fddbb7
Rebase against main branch
87176b9
Finish readme
8b431f4
Fix stuff missed during rebase.
b495c26
Really fix rebase mistake.
34dc14d
code cleanup
6b79f5d
Address review comments
6c61d99
More readme adjustments
28ce71b
Merge branch 'main' of https://github.com/openhab/openhab-addons into
d5060d0
Fix unit changes.
5d9d53c
Fix project version.
5a122ee
Fix license header dates
e07a1b1
Fix version override warning
5cd88ae
Fix author tag.
1778410
Address review comments
63ecb6d
Properly label required parameters
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
Validating CODEOWNERS rules …
This file contains 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 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 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,13 @@ | ||
This content is produced and maintained by the openHAB project. | ||
|
||
* Project home: https://www.openhab.org | ||
|
||
== Declared Project Licenses | ||
|
||
This program and the accompanying materials are made available under the terms | ||
of the Eclipse Public License 2.0 which is available at | ||
https://www.eclipse.org/legal/epl-2.0/. | ||
|
||
== Source Code | ||
|
||
https://github.com/openhab/openhab-addons |
This file contains 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,68 @@ | ||
# Govee | ||
|
||
This extension adds support for [Govee](https://www.govee.com/) Bluetooth Devices. | ||
|
||
## Supported Things | ||
|
||
Only two thing types are supported by this extension at the moment. | ||
|
||
| Thing Type ID | Description | Supported Models | | ||
|------------------------|-------------------------------------------|-------------------------------------------------------------| | ||
| goveeHygrometer | Govee Thermo-Hygrometer | H5051,H5071 | | ||
| goveeHygrometerMonitor | Govee Thermo-Hygrometer w/ Warning Alarms | H5052,H5072,H5074,H5075,H5101,H5102,H5177,H5179,B5175,B5178 | | ||
|
||
## Discovery | ||
|
||
As any other Bluetooth device, Govee devices are discovered automatically by the corresponding bridge. | ||
|
||
## Thing Configuration | ||
|
||
Govee things have the following configuration parameters: | ||
|
||
| Thing | Parameter | Required | Default | Description | | ||
|-----------------------------|-------------------------|----------|---------|-----------------------------------------------------------------------------------| | ||
| all | address | yes | | The Bluetooth address of the device (in format "XX:XX:XX:XX:XX:XX") | | ||
| all | refreshInterval | | 300 | How often, in seconds, the sensor data of the device should be refreshed | | ||
| goveeHygrometer<sup>1</sup> | temperatureCalibration | no | | Offset to apply to temperature<sup>2</sup> sensor readings | | ||
| goveeHygrometer<sup>1</sup> | humidityCalibration | no | | Offset to apply to humidity sensor readings | | ||
| goveeHygrometerMonitor | temperatureWarningAlarm | | false | Enables warning alarms to be broadcast when temperature is out of specified range | | ||
| goveeHygrometerMonitor | temperatureWarningMin | | 0 | The lower safe temperature<sup>2</sup> threshold <sup>3</sup> | | ||
| goveeHygrometerMonitor | temperatureWarningMax | | 0 | The upper safe temperature<sup>2</sup> threshold <sup>3</sup> | | ||
| goveeHygrometerMonitor | humidityWarningAlarm | | false | Enables warning alarms to be broadcast when humidity is out of specified range | | ||
| goveeHygrometerMonitor | humidityWarningMin | | 0 | The lower safe humidity threshold <sup>3</sup> | | ||
| goveeHygrometerMonitor | humidityWarningMax | | 0 | The upper safe humidity threshold <sup>3</sup> | | ||
|
||
1. Available to both `goveeHygrometer` and `goveeHygrometerMonitor` thing types. | ||
2. In °C | ||
3. Only applies if alarm feature is enabled | ||
|
||
## Channels | ||
|
||
Govee things have the following channels in addition to the default bluetooth channels: | ||
|
||
| Thing | Channel ID | Item Type | Description | | ||
|-----------------------------|------------------|------------------------|----------------------------------------------------------------| | ||
| goveeHygrometer<sup>1</sup> | temperature | Number:Temperature | The measured temperature | | ||
| goveeHygrometer<sup>1</sup> | humidity | Number:Dimensionless | The measured relative humidity | | ||
| goveeHygrometer<sup>1</sup> | battery | Number:Dimensionless | The measured battery percentage | | ||
| goveeHygrometerMonitor | temperatureAlarm | Switch | Indicates if current temperature is out of range. <sup>2</sup> | | ||
| goveeHygrometerMonitor | humidityAlarm | Switch | Indicates if current humidity is out of range. <sup>2</sup> | | ||
|
||
1. Available to both `goveeHygrometer` and `goveeHygrometerMonitor` thing types. | ||
2. Only applies if warning alarms are enabled in the configuration. | ||
|
||
## Example | ||
|
||
demo.things: | ||
|
||
``` | ||
bluetooth:goveeHygrometer:hci0:beacon "Govee Temperature Humidity Monitor" (bluetooth:bluez:hci0) [ address="12:34:56:78:9A:BC" ] | ||
``` | ||
|
||
demo.items: | ||
|
||
``` | ||
Number:Temperature temperature "Room Temperature [%.1f %unit%]" { channel="bluetooth:goveeHygrometer:hci0:beacon:temperature" } | ||
Number:Dimensionless humidity "Humidity [%.0f %unit%]" { channel="bluetooth:goveeHygrometer:hci0:beacon:humidity" } | ||
Number:Dimensionless battery "Battery [%.0f %unit%]" { channel="bluetooth:goveeHygrometer:hci0:beacon:battery" } | ||
``` |
This file contains 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,34 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.openhab.addons.bundles</groupId> | ||
<artifactId>org.openhab.addons.reactor.bundles</artifactId> | ||
<version>3.1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>org.openhab.binding.bluetooth.govee</artifactId> | ||
|
||
<name>openHAB Add-ons :: Bundles :: Govee Bluetooth Adapter</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.openhab.addons.bundles</groupId> | ||
<artifactId>org.openhab.binding.bluetooth</artifactId> | ||
<version>${project.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.openhab.addons.bundles</groupId> | ||
<artifactId>org.openhab.binding.bluetooth</artifactId> | ||
<version>${project.version}</version> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
10 changes: 10 additions & 0 deletions
10
bundles/org.openhab.binding.bluetooth.govee/src/main/feature/feature.xml
This file contains 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,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<features name="org.openhab.binding.bluetooth.govee-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0"> | ||
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository> | ||
|
||
<feature name="openhab-binding-bluetooth-govee" description="Bluetooth Binding Govee" version="${project.version}"> | ||
<feature>openhab-runtime-base</feature> | ||
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth/${project.version}</bundle> | ||
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.bluetooth.govee/${project.version}</bundle> | ||
</feature> | ||
</features> |
25 changes: 25 additions & 0 deletions
25
...g.bluetooth.govee/src/main/java/org/openhab/binding/bluetooth/gattserial/GattMessage.java
This file contains 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,25 @@ | ||
/** | ||
* Copyright (c) 2010-2021 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.bluetooth.gattserial; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* @author Connor Petty - Initial Contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public interface GattMessage { | ||
|
||
public byte[] getPayload(); | ||
} |
117 changes: 117 additions & 0 deletions
117
...ng.bluetooth.govee/src/main/java/org/openhab/binding/bluetooth/gattserial/GattSocket.java
This file contains 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,117 @@ | ||
/** | ||
* Copyright (c) 2010-2021 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.bluetooth.gattserial; | ||
|
||
import java.util.Deque; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionException; | ||
import java.util.concurrent.ConcurrentLinkedDeque; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Consumer; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
/** | ||
* @author Connor Petty - Initial Contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public abstract class GattSocket<T extends GattMessage, R extends GattMessage> { | ||
|
||
private static final Future<?> COMPLETED_FUTURE = CompletableFuture.completedFuture(null); | ||
|
||
private final Deque<MessageProcessor> messageProcessors = new ConcurrentLinkedDeque<>(); | ||
|
||
public void registerMessageHandler(MessageHandler<T, R> messageHandler) { | ||
// we need to use a dummy future since ConcurrentHashMap doesn't allow null values | ||
messageProcessors.addFirst(new MessageProcessor(messageHandler, COMPLETED_FUTURE)); | ||
} | ||
|
||
protected abstract ScheduledExecutorService getScheduler(); | ||
|
||
public void sendMessage(MessageServicer<T, R> messageServicer) { | ||
T message = messageServicer.createMessage(); | ||
|
||
CompletableFuture<@Nullable Void> messageFuture = sendMessage(message); | ||
|
||
Future<?> timeoutFuture = getScheduler().schedule(() -> { | ||
messageFuture.completeExceptionally(new TimeoutException("Timeout while waiting for response")); | ||
}, messageServicer.getTimeout(TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS); | ||
|
||
MessageProcessor processor = new MessageProcessor(messageServicer, timeoutFuture); | ||
messageProcessors.addLast(processor); | ||
|
||
messageFuture.whenComplete((v, ex) -> { | ||
if (ex instanceof CompletionException) { | ||
ex = ex.getCause(); | ||
} | ||
if (ex != null) { | ||
if (messageServicer.handleFailedMessage(message, ex)) { | ||
timeoutFuture.cancel(false); | ||
messageProcessors.remove(processor); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public CompletableFuture<@Nullable Void> sendMessage(T message) { | ||
List<byte[]> packets = createPackets(message); | ||
var futures = packets.stream()// | ||
.map(this::sendPacket)// | ||
.toArray(CompletableFuture[]::new); | ||
|
||
return CompletableFuture.allOf(futures); | ||
} | ||
|
||
protected List<byte[]> createPackets(T message) { | ||
return List.of(message.getPayload()); | ||
} | ||
|
||
protected abstract void parsePacket(byte[] packet, Consumer<R> messageHandler); | ||
|
||
protected abstract CompletableFuture<@Nullable Void> sendPacket(byte[] value); | ||
|
||
public void receivePacket(byte[] packet) { | ||
parsePacket(packet, this::handleMessage); | ||
} | ||
|
||
private void handleMessage(R message) { | ||
for (Iterator<MessageProcessor> it = messageProcessors.iterator(); it.hasNext();) { | ||
MessageProcessor processor = it.next(); | ||
if (processor.messageHandler.handleReceivedMessage(message)) { | ||
processor.timeoutFuture.cancel(false); | ||
it.remove(); | ||
// we want to return after the first message servicer handles the message | ||
if (processor.timeoutFuture != COMPLETED_FUTURE) { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private class MessageProcessor { | ||
private MessageHandler<T, R> messageHandler; | ||
private Future<?> timeoutFuture; | ||
|
||
public MessageProcessor(MessageHandler<T, R> messageHandler, Future<?> timeoutFuture) { | ||
this.messageHandler = messageHandler; | ||
this.timeoutFuture = timeoutFuture; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...luetooth.govee/src/main/java/org/openhab/binding/bluetooth/gattserial/MessageHandler.java
This file contains 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,37 @@ | ||
/** | ||
* Copyright (c) 2010-2021 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.bluetooth.gattserial; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* @author Connor Petty - Initial Contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public interface MessageHandler<T extends GattMessage, R extends GattMessage> { | ||
|
||
/** | ||
* | ||
* @param payload | ||
* @return true if this handler should be removed from the handler list | ||
*/ | ||
public boolean handleReceivedMessage(R message); | ||
|
||
/** | ||
* | ||
* @param payload | ||
* @return true if this handler should be removed from the handler list | ||
*/ | ||
public boolean handleFailedMessage(T message, Throwable th); | ||
} |
28 changes: 28 additions & 0 deletions
28
...uetooth.govee/src/main/java/org/openhab/binding/bluetooth/gattserial/MessageServicer.java
This file contains 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,28 @@ | ||
/** | ||
* Copyright (c) 2010-2021 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.bluetooth.gattserial; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* @author Connor Petty - Initial Contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public interface MessageServicer<T extends GattMessage, R extends GattMessage> | ||
extends MessageHandler<T, R>, MessageSupplier<T> { | ||
|
||
public long getTimeout(TimeUnit unit); | ||
} |
22 changes: 22 additions & 0 deletions
22
...uetooth.govee/src/main/java/org/openhab/binding/bluetooth/gattserial/MessageSupplier.java
This file contains 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,22 @@ | ||
/** | ||
* Copyright (c) 2010-2021 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.bluetooth.gattserial; | ||
|
||
/** | ||
* @author Connor Petty - Initial Contribution | ||
* | ||
*/ | ||
public interface MessageSupplier<M extends GattMessage> { | ||
|
||
public M createMessage(); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You use "extension", "adapter" and "binding" for this code in different places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is kinda the case for all of the bluetooth bindings...
"extension" refers to the fact that the binding adds functionality to the base bluetooth bindings.
"adapter" is used the pom.xml as part of the project name. But strictly speaking I don't really think this is appropriate and the word should probably be changed to
extension
instead. But this standard was in place for all of the other bluetooth bindings so if I'm going to change it it should be a separate PR.I do agree with you that these naming conventions are a bit confusing and I would love to change them at some point.