Skip to content
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

Added current state sensor + other improvements #5

Merged
merged 4 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 50 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
# flic2hass
A Flic SDK utility to publish all Flic buttons to Home Assistant via MQTT
## A Flic SDK utility to publish all of your Flic buttons to Home Assistant via MQTT

Utilizing https://hubsdk.flic.io/static/tutorial/
Requirements:
* A Flic Hub
* A functional MQTT server
## Basic Steps:

Your Flic Hub online IDE: https://hubsdk.flic.io/
**1. Connect to Flic Hub IDE:**

Follow the tutorial steps above, substituting in main.js -- DON'T FORGET TO UPDATE IT TO USE YOUR OWN MQTT SERVER!
* Follow along with the beginning of [these instructions](https://hubsdk.flic.io/static/tutorial/) to enable SDK access.
* Go to: <https://hubsdk.flic.io/> and login, your hub should be discovered automatically.

Requirements:
* Flic Hub
* Working MQTT system
* Enough know-how to get it to work - you will need to also follow the tutorial a bit (linked above)

Basic steps:
* Connect to your Flic Hub IDE
* Create a new module, name it MQTT
* Paste in the main.js and mqtt.js files
* Update the main.js variables server and hatopic.
* IF you use a username/password on your MQTT, set that up:
> in main.js
>> var username = 'flichub';
>> var password = 'xxxxx';
> and change this line....
>> var mqtt = require("./mqtt").create(server,{'username':username,'password':password});

* Start the "Module" in the IDE and watch the Console output - it'se extremely verbose right now

* IF it started right, set the "restart after crash" checkbox just in case

* IF it didn't start, try powercycling your Flic Hub and reconnect. Verify the Module saved properly and is running.
**2. Create `MQTT` module:**

* One in the Web IDE, click "Create Module".
* Give the new module a name. "`MQTT`" is a good option but anything will work.

**3. Insert `main.js` and `mqtt.js`:**

* Copy content from `main.js` in this repo to main.js in the flic IDE.
* Right click the folder in the left pane and select "New File".
* Name the file `mqtt.js` (IT MUST BE NAMED THIS).
* Copy content from `mqtt.js` in this repo to mqtt.js in the flic IDE.

**4. Update variables in `main.js`:**

* Modify `server`, `hatopic`, `authentication` with your details.

*If your MQTT server does not require authentication:*

* Delete:

```javascript
var username = 'flichub';
var password = 'xxxxx';
```

* Replace:

```javascript
var mqtt = require("./mqtt").create(server,{'username':username,'password':password});
```

With:

```javascript
var mqtt = require("./mqtt").create(server);
```

1. Start the module in the IDE by clicking the green play button, and watch the Console output (it's extremely verbose right now)

*If the module didn't start correctly, try powercycling your Flic Hub and reconnect. Verify the Module saved properly and is running.*

2. Once the module has started and you have verified it is working as expected, turn on the "restart after crash" checkbox to ensure the module is always running after any unexpected crash or hub power cycle.
43 changes: 37 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var server = "mqtt.home";
var hatopic = "homeassistant";
var flictopic = "flic";
var username = "";
var password = "";
var mqtt = require("./mqtt").create(server,{'username':username,'password':password});

var buttonManager = require("buttons");

var mqtt = require("./mqtt").create(server);

var myButtons = {}; //Dictionary of Button objects

//This runs when a button is added via Flic's interface
Expand Down Expand Up @@ -86,7 +86,7 @@ buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) {

var clickType = obj.isSingleClick ? "click" : obj.isDoubleClick ? "double_click" : "hold";
var sn = button.serialNumber;

if (!myButtons[button.bdaddr]) {
console.log("**** Found an unregistered button. It must be new! ***")
register_button(button);
Expand All @@ -106,6 +106,25 @@ buttonManager.on("buttonSingleOrDoubleClickOrHold", function(obj) {
console.log(btntopic+"/battery: \t"+button.batteryStatus);
});

buttonManager.on("buttonUp", function(obj) {
var button = buttonManager.getButton(obj.bdaddr);
console.log("\nButton Released! " + button.serialNumber + " " + button.name);
publishButtonState(button, "released");
});

buttonManager.on("buttonDown", function(obj) {
var button = buttonManager.getButton(obj.bdaddr);
console.log("\nButton Pressed! " + button.serialNumber + " " + button.name);
publishButtonState(button, "pressed");
});

function publishButtonState(button, state) {
var sn = button.serialNumber;
var statetopic = flictopic + "/" + sn + "/state";
mqtt.publish(statetopic, state);
console.log(statetopic + ": \t" + state);
}

function register_button(button) {
//Register one button
//This sets up the MQTT Discovery topics to publish the Flic button
Expand Down Expand Up @@ -137,7 +156,7 @@ function register_button(button) {
};

//Setup config and destination for button press
obj.name = button.name + " Flic Button";
obj.name = button.name + " Button Action";
obj.state_topic = buttontopic + "/action";
obj.unique_id = "Flic_" + button.serialNumber + "_action";

Expand All @@ -147,11 +166,23 @@ function register_button(button) {
retain: true
});

// Setup config and destination for button state
obj.name = button.name + " Button State";
obj.state_topic = buttontopic + "/state";
obj.unique_id = "Flic_" + button.serialNumber + "_state";

payload = JSON.stringify(obj, null, 4);
console.log(configtopic + "/state/config:\t" + payload);
mqtt.publish(configtopic + "/state/config", payload, {
retain: true
});

//Setup config and destination for battery level report
obj.name = button.name + " Flic Button Battery Level";
obj.name = button.name + " Battery Level";
obj.state_topic = buttontopic + "/battery";
obj.unique_id = "Flic_" + button.serialNumber + "_battery";
obj.device_class = "battery";
obj.entity_category = "diagnostic";
//obj.unit_of_measurement = "%"; //It doesn't seem to actually like this.

payload = JSON.stringify(obj, null, 4);
Expand Down