Skip to content

Commit

Permalink
[flicbutton] Initial contribution FlicButton Binding (openhab#9234)
Browse files Browse the repository at this point in the history
* [flicbutton] Initial contribution FlicButton Binding

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Add config parameter address for FlicButton thing

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Run spotless

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Code cleanup & docs improvement

Signed-off-by: Patrick Fink <mail@pfink.de>

* Apply suggestions from code review

Co-authored-by: Fabian Wolter <github@fabian-wolter.de>

* [flicbutton] Update LICENSE

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Apply suggestions from code review (2) & update to 3.1-SNAPSHOT

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Apply suggestions from code review (3) & fix offline status

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Fix 3rd party source for proper IDE integration

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Simplify config parsing

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Move everything to internal package

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Remove hyphens from port parameter docs example

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Change maintainer to openHAB project

Signed-off-by: Patrick Fink <mail@pfink.de>

* Apply docs suggestions + update to 3.2.0-SNAPSHOT

Signed-off-by: Patrick Fink <mail@pfink.de>

Co-authored-by: Matthew Skinner <matt@pcmus.com>

* [flicbutton] Fix bridge offline & reconnect handling

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Close open socket on dispose

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Improve exception error message in ThingStatus

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Fix README title

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Improve exception error message in ThingStatus

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Style fixes

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Use trace log level for button clicks & status changes

Signed-off-by: Patrick Fink <mail@pfink.de>

* Apply doc improvements from code review

Signed-off-by: Patrick Fink <mail@pfink.de>

Co-authored-by: Matthew Skinner <matt@pcmus.com>

* [flicbutton] Add binding to bom/openhab-addons

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Cleanup / remove guava leftover

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Remove online status description

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Improve flicd hostname label

Signed-off-by: Patrick Fink <mail@pfink.de>

Co-authored-by: Fabian Wolter <github@fabian-wolter.de>

* [flicbutton] Do not catch IllegalArgumentException anymore as its not neeed

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Use debug log level instead of info

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Update version and license

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Fix SAT warnings, e.g. add null handling annotations

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Fix SAT warnings (2)

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Concurrency refactoring & fixes

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Cancel initialization task also when already running

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Add javadoc and move FLIC_OPENHAB_EVENT_TRIGGER_MAP constant to constants class

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Use ThingStatusDetail.OFFLINE.GONE when Flic button was removed from bridge

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Fix FlicSimpleclientDiscoveryServiceImpl javadoc

Signed-off-by: Patrick Fink <mail@pfink.de>

* [flicbutton] Fix required definition of thing types

Signed-off-by: Patrick Fink <mail@pfink.de>

Co-authored-by: Fabian Wolter <github@fabian-wolter.de>
Co-authored-by: Matthew Skinner <matt@pcmus.com>
  • Loading branch information
3 people authored and andan67 committed Nov 5, 2022
1 parent 6929a43 commit 9d81a0f
Show file tree
Hide file tree
Showing 42 changed files with 3,149 additions and 0 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
/bundles/org.openhab.binding.exec/ @kgoderis
/bundles/org.openhab.binding.feed/ @svilenvul
/bundles/org.openhab.binding.feican/ @Hilbrand
/bundles/org.openhab.binding.flicbutton/ @pfink
/bundles/org.openhab.binding.fmiweather/ @ssalonen
/bundles/org.openhab.binding.folderwatcher/ @goopilot
/bundles/org.openhab.binding.folding/ @fa2k
Expand Down
5 changes: 5 additions & 0 deletions bom/openhab-addons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,11 @@
<artifactId>org.openhab.binding.feican</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.flicbutton</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.fmiweather</artifactId>
Expand Down
21 changes: 21 additions & 0 deletions bundles/org.openhab.binding.flicbutton/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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

== Third-party Content

fliclib-javaclient (files under src/3rdparty)

* License: CC0 1.0
* Project: https://github.com/50ButtonsEach/fliclib-linux-hci
* Source: https://github.com/50ButtonsEach/fliclib-linux-hci/tree/master/clientlib/java/lib/src/main/java/io/flic/fliclib/javaclient
130 changes: 130 additions & 0 deletions bundles/org.openhab.binding.flicbutton/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Flic Button Binding

openHAB binding for using [Flic Buttons](https://flic.io/)
with a [fliclib-linux-hci](https://github.com/50ButtonsEach/fliclib-linux-hci) bridge.

Currently, although Flic Buttons are BLE devices, this binding only supports fliclib-linux-hci (flicd) as a bridge.
The openHAB Bluetooth Bindings are not supported.
Flicd requires a seperate Bluetooth adapter to work, so if you use this binding together with e.g. the
[Bluez Binding](https://www.openhab.org/addons/bindings/bluetooth.bluez/),
two physical Bluetooth adapters are required (one for Bluez and one for flicd).
Be aware that flicd requires an initial internet connection for the verification of the buttons.
After buttons are initially added to flicd, an internet connection is not required anymore.

## Supported Things

| Thing Type ID | Description |
| --------------- | ------------------------- |
| flicd-bridge | The bridge representing a running instance of [fliclib-linux-hci (flicd)](https://github.com/50ButtonsEach/fliclib-linux-hci) on the server. |
| button | The Flic button (supports Flic 1 buttons as well as Flic 2 buttons) |

## Discovery

* There is no automatic discovery for flicd-bridge available.
* After flicd-bridge is (manually) configured, buttons will be automatically discovered via background discovery as soon
as they're added with [simpleclient](https://github.com/50ButtonsEach/fliclib-linux-hci).

If they're already attached to the flicd-bridge before configuring this binding, they can be discovered by triggering an
active scan.

## Thing Configuration

### flicd-bridge

Example for textual configuration:

```
Bridge flicbutton:flicd-bridge:mybridge
```

The default host is localhost:5551 (this should be sufficient if flicd is running with default settings on the same server as openHAB).
If your flicd service is running somewhere else, specify it like this:

```
Bridge flicbutton:flicd-bridge:mybridge [ hostname="<YOUR_HOSTNAME>", port=<YOUR_PORT>]
```

If flicd is running on a remote host, please do not forget to start it with the parameter `-s <openHAB IP>`, otherwise it won't be accessible for openHAB (more details on [fliclib-linux-hci](https://github.com/50ButtonsEach/fliclib-linux-hci)).

### button

For the button, the only config parameter is the MAC address.
Normally, no textual configuration is necessary as buttons are auto discovered as soon as the bridge is configured.
If you want to use textual configuration anyway, you can do it like this:

```
Bridge flicbutton:flicd-bridge:mybridge [ hostname="<YOUR_HOSTNAME>", port=<YOUR_PORT>] {
Thing button myflic1 "<YOUR_LABEL>" [address ="<MAC_ADDRESS>"]
Thing button myflic2 "<YOUR_LABEL>" [address ="<MAC_ADDRESS>"]
...
}
```

You can lookup the MAC addresses of your buttons within the inbox of the UI.
You're free to choose any label you like for your button.

## Channels

| Channel ID | Channel Type | Item Type | Description |
| ------------------------- | ------------------------ | --------------------------| ------------------------------ |
| rawbutton | [System Trigger Channel](https://www.openhab.org/docs/developer/bindings/thing-xml.html#system-trigger-channel-types) `system.rawbutton` | Depends on the [Trigger Profile](https://www.openhab.org/docs/configuration/items.html#profiles) used | Raw Button channel that triggers `PRESSED` and `RELEASED` events, allows to use openHAB profiles or own implementations via rules to detect e.g. double clicks, long presses etc. |
| button | [System Trigger Channel](https://www.openhab.org/docs/developer/bindings/thing-xml.html#system-trigger-channel-types) `system.button` | Depends on the [Trigger Profile](https://www.openhab.org/docs/configuration/items.html#profiles) used | Button channel that is using Flic's implementation for detecting long, short or double clicks. Triggers `SHORT_PRESSED`,`DOUBLE_PRESSED` and `LONG_PRESSED` events. |
| battery-level | [System State Channel](https://www.openhab.org/docs/developer/bindings/thing-xml.html#system-state-channel-types) `system.battery-level` | Number | Represents the battery level as a percentage (0-100%).
## Example

### Initial setup

1. Setup and run flicd as described in [fliclib-linux-hci](https://github.com/50ButtonsEach/fliclib-linux-hci).
Please consider that you need a separate Bluetooth adapter. Shared usage with other Bluetooth services (e.g. Bluez)
is not possible.
1. Connect your buttons to flicd using the simpleclient as described in
[fliclib-linux-hci](https://github.com/50ButtonsEach/fliclib-linux-hci). Flicd has to run in background the whole
time, simpleclient can be killed after you successfully test the button connects.
1. Add a flicd-bridge via the UI or textual configuration. Please consider that flicd only accepts connections from
localhost by default, to enable remote connections from openHAB you have to use the `--server-addr` parameter as
described in [fliclib-linux-hci](https://github.com/50ButtonsEach/fliclib-linux-hci).
1. When the bridge is online, buttons newly added via simpleclient will automatically get discovered via background
discovery. To discover buttons that were set up before the binding was setup, please run an active scan.

### Configuration Example using Profiles

[Profiles](https://www.openhab.org/docs/configuration/items.html#profiles) are the recommended way to use this binding.

demo.things:

```
Bridge flicbutton:flicd-bridge:local-flicd {
Thing button flic_livingroom "Yellow Button Living Room" [address = "60:13:B3:02:18:BD"]
Thing button flic_kitchen "Black Button Kitchen" [address = "B5:7E:59:78:86:9F"]
}
```

demo.items:

```
Dimmer Light_LivingRoom { channel="milight:rgbLed:milight2:4:ledbrightness", channel="flicbutton:button:local-flicd:flic_livingroom:rawbutton" [profile="rawbutton-toggle-switch"], channel="flicbutton:button:local-flicd:flic_kitchen:rawbutton" [profile="rawbutton-toggle-switch"] } // We have a combined kitchen / livingroom, so we control the living room lights with switches from the living room and from the kitchen
Switch Light_Kitchen { channel="hue:group:1:kitchen-bulbs:switch", channel="flicbutton:button:local-flicd:flic_kitchen:rawbutton" [profile="rawbutton-toggle-switch"] }
```

### Configuration Example using Rules

It's also possible to setup [Rules](https://www.openhab.org/docs/configuration/rules-dsl.html).
The following rules help to initially test your setup as they'll trigger log messages on incoming events.

```
rule "Button rule using the button channel"
when
Channel "flicbutton:button:local-flicd:flic_livingroom:button" triggered SHORT_PRESSED
then
logInfo("Flic", "Flic 'short pressed' triggered")
end
rule "Button rule directly using the rawbutton channel"
when
Channel "flicbutton:button:local-flicd:flic_livingroom:rawbutton" triggered
then
logInfo("Flic", "Flic pressed: " + receivedEvent.event)
end
```
38 changes: 38 additions & 0 deletions bundles/org.openhab.binding.flicbutton/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.3.0-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.binding.flicbutton</artifactId>

<name>openHAB Add-ons :: Bundles :: FlicButton Binding</name>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>add-source</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<sources>
<source>src/3rdparty/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
121 changes: 121 additions & 0 deletions bundles/org.openhab.binding.flicbutton/src/3rdparty/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
Creative Commons Legal Code

CC0 1.0 Universal

CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
HEREUNDER.

Statement of Purpose

The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work of
authorship and/or a database (each, a "Work").

Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without fear
of later claims of infringement build upon, modify, incorporate in other
works, reuse and redistribute as freely as possible in any form whatsoever
and for any purposes, including without limitation commercial purposes.
These owners may contribute to the Commons to promote the ideal of a free
culture and the further production of creative, cultural and scientific
works, or to gain reputation or greater distribution for their Work in
part through the use and efforts of others.

For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under its
terms, with knowledge of his or her Copyright and Related Rights in the
Work and the meaning and intended legal effect of CC0 on those rights.

1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights ("Copyright and
Related Rights"). Copyright and Related Rights include, but are not
limited to, the following:

i. the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
ii. moral rights retained by the original author(s) and/or performer(s);
iii. publicity and privacy rights pertaining to a person's image or
likeness depicted in a Work;
iv. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
v. rights protecting the extraction, dissemination, use and reuse of data
in a Work;
vi. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation
thereof, including any amended or successor version of such
directive); and
vii. other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.

2. Waiver. To the greatest extent permitted by, but not in contravention
of, applicable law, Affirmer hereby overtly, fully, permanently,
irrevocably and unconditionally waives, abandons, and surrenders all of
Affirmer's Copyright and Related Rights and associated claims and causes
of action, whether now known or unknown (including existing as well as
future claims and causes of action), in the Work (i) in all territories
worldwide, (ii) for the maximum duration provided by applicable law or
treaty (including future time extensions), (iii) in any current or future
medium and for any number of copies, and (iv) for any purpose whatsoever,
including without limitation commercial, advertising or promotional
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
member of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal or
equitable action to disrupt the quiet enjoyment of the Work by the public
as contemplated by Affirmer's express Statement of Purpose.

3. Public License Fallback. Should any part of the Waiver for any reason
be judged legally invalid or ineffective under applicable law, then the
Waiver shall be preserved to the maximum extent permitted taking into
account Affirmer's express Statement of Purpose. In addition, to the
extent the Waiver is so judged Affirmer hereby grants to each affected
person a royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer's Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future
time extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"License"). The License shall be deemed effective as of the date CC0 was
applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder
of the License, and in such case Affirmer hereby affirms that he or she
will not (i) exercise any of his or her remaining Copyright and Related
Rights in the Work or (ii) assert any associated claims and causes of
action with respect to the Work, in either case contrary to Affirmer's
express Statement of Purpose.

4. Limitations and Disclaimers.

a. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
b. Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy, or
the present or absence of errors, whether or not discoverable, all to
the greatest extent permissible under applicable law.
c. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person's Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the
Work.
d. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to
this CC0 or use of the Work.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.flic.fliclib.javaclient;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Battery status listener.
*
* Add this listener to a {@link FlicClient} by executing {@link FlicClient#addBatteryStatusListener(BatteryStatusListener)}.
*/
public class BatteryStatusListener {
private static AtomicInteger nextId = new AtomicInteger();
int listenerId = nextId.getAndIncrement();

private Bdaddr bdaddr;
Callbacks callbacks;

public BatteryStatusListener(Bdaddr bdaddr, Callbacks callbacks) {
if (bdaddr == null) {
throw new IllegalArgumentException("bdaddr is null");
}
if (callbacks == null) {
throw new IllegalArgumentException("callbacks is null");
}
this.bdaddr = bdaddr;
this.callbacks = callbacks;
}

public Bdaddr getBdaddr() {
return bdaddr;
}

public abstract static class Callbacks {
/**
* This will be called when the battery status has been updated.
* It will also be called immediately after the battery status listener has been created.
* If the button stays connected, this method will be called approximately every three hours.
*
* @param bdaddr Bluetooth device address
* @param batteryPercentage A number between 0 and 100 for the battery level. Will be -1 if unknown.
* @param timestamp Standard UNIX timestamp, in seconds, for the event.
*/
public abstract void onBatteryStatus(Bdaddr bdaddr, int batteryPercentage, long timestamp) throws IOException;
}
}
Loading

0 comments on commit 9d81a0f

Please sign in to comment.