Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2935827f60 | ||
|
|
bf4e69e2ce | ||
|
|
df74989fdd | ||
|
|
969b8bd0e2 | ||
|
|
ec295d3f8a | ||
|
|
c97c9c494c | ||
|
|
ecc223d6e0 |
No files matched your search
@@ -1,12 +1,15 @@
|
|||||||
# Flic2Hass 
|
# Flic2Hass 
|
||||||
|
|
||||||
|
|
||||||
|
> <a href='https://ko-fi.com/J3J8C4RB7' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi1.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
|
||||||
|
|
||||||
This is a complete refactor of various previous versions of flic2hass. This version provides more features and even supports the onboard IR module!
|
This is a complete refactor of various previous versions of flic2hass. This version provides more features and even supports the onboard IR module!
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
### Complete Overview of Buttons
|
### Complete Overview of Buttons
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
* Show connectivity state
|
* Show connectivity state
|
||||||
* Show click action types: `click` `hold` `double_click`
|
* Show click action types: `click` `hold` `double_click`
|
||||||
@@ -20,7 +23,7 @@ This is a complete refactor of various previous versions of flic2hass. This vers
|
|||||||
* Record remote signals
|
* Record remote signals
|
||||||
* Send remote signals
|
* Send remote signals
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Requirements:
|
Requirements:
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 164 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 163 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 136 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 140 KiB |
@@ -926,6 +926,11 @@ MQTT.prototype.packetHandler = function(data) {
|
|||||||
this.partData = data;
|
this.partData = data;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Allow zero-size pData, but detect if its indexes
|
||||||
|
// will go out of bounds.
|
||||||
|
if (1 + dLen.lenBy >= data.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Get the data for this packet
|
// Get the data for this packet
|
||||||
var pData = data.slice(1 + dLen.lenBy, pLen);
|
var pData = data.slice(1 + dLen.lenBy, pLen);
|
||||||
// more than one packet? re-emit it so we handle it later
|
// more than one packet? re-emit it so we handle it later
|
||||||
@@ -1088,7 +1093,7 @@ MQTT.prototype.packetHandler = function(data) {
|
|||||||
retain: bool // the server should retain this message and send it out again to new subscribers
|
retain: bool // the server should retain this message and send it out again to new subscribers
|
||||||
dup : bool // indicate the message is a duplicate because original wasn't ACKed (QoS > 0 only)
|
dup : bool // indicate the message is a duplicate because original wasn't ACKed (QoS > 0 only)
|
||||||
}
|
}
|
||||||
*/ MQTT.prototype.publish = function(topic, message, opts) {
|
*/ MQTT.prototype.publish = function(topic, message, opts) {
|
||||||
if (!this.client) return;
|
if (!this.client) return;
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
try {
|
try {
|
||||||
|
|||||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "flic2hass",
|
"name": "flic2hass",
|
||||||
"version": "1.0.0",
|
"version": "1.1.0",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "flic2hass",
|
"name": "flic2hass",
|
||||||
"version": "1.0.0",
|
"version": "1.1.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@swc/cli": "^0.1.62",
|
"@swc/cli": "^0.1.62",
|
||||||
|
|||||||
+20
-19
@@ -1,9 +1,10 @@
|
|||||||
import { ButtonControllerOpt, makeButtonController } from "./ButtonController";
|
import {ButtonControllerOpt, makeButtonController} from "./ButtonController";
|
||||||
import { HAmqttOptions, makeHAmqtt } from './HAmqtt';
|
import {HAmqttOptions, makeHAmqtt} from './HAmqtt';
|
||||||
import { IRControllerOpt, makeIRController } from "./IRController";
|
import {IRControllerOpt, makeIRController} from "./IRController";
|
||||||
import { makeLogger } from "./Logger";
|
import {makeLogger} from "./Logger";
|
||||||
import { ButtonModule, IRModule } from './flicTypes';
|
import {ButtonModule, IRModule} from './flicTypes';
|
||||||
import * as mqtt from './mqtt';
|
import * as mqtt from './mqtt';
|
||||||
|
|
||||||
export type MQTTOpt = {
|
export type MQTTOpt = {
|
||||||
host: string;
|
host: string;
|
||||||
port: number;
|
port: number;
|
||||||
@@ -27,18 +28,18 @@ export const start = (
|
|||||||
) => {
|
) => {
|
||||||
const mqttServer = mqtt.create(
|
const mqttServer = mqtt.create(
|
||||||
options.mqtt.host,
|
options.mqtt.host,
|
||||||
{ ...options.mqtt, keep_alive: true },
|
{...options.mqtt, keep_alive: true},
|
||||||
);
|
);
|
||||||
const logger = makeLogger('root', options.debug ?? false);
|
const logger = makeLogger('root', options.debug ?? false);
|
||||||
options.ha = options.ha ?? {}
|
options.ha = options.ha ?? {};
|
||||||
options.flicBtns = options.flicBtns ?? {}
|
options.flicBtns = options.flicBtns ?? {};
|
||||||
options.flicIR = options.flicIR ?? {}
|
options.flicIR = options.flicIR ?? {};
|
||||||
options.ha.debug = options.ha.debug ?? options.debug ?? false
|
options.ha.debug = options.ha.debug ?? options.debug ?? false;
|
||||||
options.flicBtns.debug = options.flicBtns.debug ?? options.debug ?? false
|
options.flicBtns.debug = options.flicBtns.debug ?? options.debug ?? false;
|
||||||
options.flicIR.debug = options.flicBtns.debug ?? options.debug ?? false
|
options.flicIR.debug = options.flicIR.debug ?? options.debug ?? false;
|
||||||
const ha = makeHAmqtt(mqttServer, options.ha);
|
const ha = makeHAmqtt(mqttServer, options.ha);
|
||||||
mqttServer.on('connected', () => {
|
mqttServer.on('connected', () => {
|
||||||
logger.info("connected to mqtt")
|
logger.info("connected to mqtt");
|
||||||
if (!options.flicBtns?.disabled) {
|
if (!options.flicBtns?.disabled) {
|
||||||
makeButtonController(
|
makeButtonController(
|
||||||
ha, buttonModule, options.flicBtns,
|
ha, buttonModule, options.flicBtns,
|
||||||
@@ -52,25 +53,25 @@ export const start = (
|
|||||||
options.flicIR,
|
options.flicIR,
|
||||||
).start();
|
).start();
|
||||||
}
|
}
|
||||||
logger.info("all services up!")
|
logger.info("all services up!");
|
||||||
});
|
});
|
||||||
mqttServer.on('error', function (err) {
|
mqttServer.on('error', function (err) {
|
||||||
logger.info("'Error' event", JSON.stringify(err));
|
logger.info("'Error' event", JSON.stringify(err));
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
throw new Error("Crashed")
|
throw new Error("Crashed");
|
||||||
}, 1000);
|
}, 1000);
|
||||||
});
|
});
|
||||||
mqttServer.on("disconnected", function (err) {
|
mqttServer.on("disconnected", function (err) {
|
||||||
logger.info("'Error' disconnected", JSON.stringify(err));
|
logger.info("'Error' disconnected", JSON.stringify(err));
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
throw new Error("Crashed")
|
throw new Error("Crashed");
|
||||||
}, 1000);
|
}, 1000);
|
||||||
});
|
});
|
||||||
mqttServer.on("close", function (err) {
|
mqttServer.on("close", function (err) {
|
||||||
logger.info("'Error' close", JSON.stringify(err));
|
logger.info("'Error' close", JSON.stringify(err));
|
||||||
setTimeout(function () {
|
setTimeout(function () {
|
||||||
throw new Error("Crashed")
|
throw new Error("Crashed");
|
||||||
}, 1000);
|
}, 1000);
|
||||||
});
|
});
|
||||||
mqttServer.connect()
|
mqttServer.connect();
|
||||||
}
|
};
|
||||||
+38
-14
@@ -57,7 +57,7 @@ var pakId = Math.floor(Math.random() * 65534);
|
|||||||
|
|
||||||
Uint8Array.prototype.charCodeAt = function (a, b) {
|
Uint8Array.prototype.charCodeAt = function (a, b) {
|
||||||
return this.toString().charCodeAt(a, b);
|
return this.toString().charCodeAt(a, b);
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Return Codes
|
Return Codes
|
||||||
@@ -136,7 +136,7 @@ Object.prototype.on = function (type, fn) {
|
|||||||
__listeners[type] = [];
|
__listeners[type] = [];
|
||||||
}
|
}
|
||||||
__listeners[type].push(fn);
|
__listeners[type].push(fn);
|
||||||
}
|
};
|
||||||
|
|
||||||
Object.prototype.emit = function (type, ...data) {
|
Object.prototype.emit = function (type, ...data) {
|
||||||
if (__listeners[type]) {
|
if (__listeners[type]) {
|
||||||
@@ -144,12 +144,12 @@ Object.prototype.emit = function (type, ...data) {
|
|||||||
fn(...data);
|
fn(...data);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
if (!Buffer.from) {
|
if (!Buffer.from) {
|
||||||
Buffer.from = function (a) {
|
Buffer.from = function (a) {
|
||||||
return new Buffer(a);
|
return new Buffer(a);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 'public' constants here */
|
/** 'public' constants here */
|
||||||
@@ -181,9 +181,9 @@ export function mqttStr(s) {
|
|||||||
export function mqttInt2Str(arr: number[] | Uint8Array): string {
|
export function mqttInt2Str(arr: number[] | Uint8Array): string {
|
||||||
let outStr = "";
|
let outStr = "";
|
||||||
for (var i = 0; i < arr.length; i++) {
|
for (var i = 0; i < arr.length; i++) {
|
||||||
outStr += createEscapedHex(arr[i])
|
outStr += createEscapedHex(arr[i]);
|
||||||
}
|
}
|
||||||
return outStr
|
return outStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** MQTT packet length formatter - algorithm from reference docs */
|
/** MQTT packet length formatter - algorithm from reference docs */
|
||||||
@@ -211,8 +211,8 @@ function mqttPacketLengthDec(length) {
|
|||||||
do {
|
do {
|
||||||
lb = length[bytes];
|
lb = length[bytes];
|
||||||
decL |= (lb & 127) << (bytes++ * 7);
|
decL |= (lb & 127) << (bytes++ * 7);
|
||||||
} while ((lb & 128) && (bytes < 4))
|
} while ((lb & 128) && (bytes < 4));
|
||||||
return { "decLen": decL, "lenBy": bytes };
|
return {"decLen": decL, "lenBy": bytes};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** MQTT standard packet formatter */
|
/** MQTT standard packet formatter */
|
||||||
@@ -310,8 +310,16 @@ MQTT.prototype.packetHandler = function (data) {
|
|||||||
this.partData = data;
|
this.partData = data;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Allow zero-size pData, but detect if its indexes
|
||||||
|
// will go out of bounds.
|
||||||
|
if (1 + dLen.lenBy >= data.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Get the data for this packet
|
// Get the data for this packet
|
||||||
var pData = data.slice(1 + dLen.lenBy, pLen);
|
var pData = data.slice(1 + dLen.lenBy, pLen);
|
||||||
|
|
||||||
// more than one packet? re-emit it so we handle it later
|
// more than one packet? re-emit it so we handle it later
|
||||||
if (data.length > pLen) {
|
if (data.length > pLen) {
|
||||||
this.client.emit('data', data.slice(pLen, data.length));
|
this.client.emit('data', data.slice(pLen, data.length));
|
||||||
@@ -402,7 +410,7 @@ MQTT.prototype.connect = function (client) {
|
|||||||
var onConnect = function () {
|
var onConnect = function () {
|
||||||
mqo.client = client;
|
mqo.client = client;
|
||||||
// write connection message
|
// write connection message
|
||||||
var teststring = mqo.mqttConnect(mqo.client_id)
|
var teststring = mqo.mqttConnect(mqo.client_id);
|
||||||
client.write(teststring);
|
client.write(teststring);
|
||||||
// handle connection timeout if too slow
|
// handle connection timeout if too slow
|
||||||
mqo.ctimo = setTimeout(function () {
|
mqo.ctimo = setTimeout(function () {
|
||||||
@@ -422,7 +430,7 @@ MQTT.prototype.connect = function (client) {
|
|||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
var self = this;
|
var self = this;
|
||||||
client = require("net").Socket().connect({ host: mqo.server, port: mqo.port }, onConnect);
|
client = require("net").Socket().connect({host: mqo.server, port: mqo.port}, onConnect);
|
||||||
client.on('error', function (err) {
|
client.on('error', function (err) {
|
||||||
self.emit('error', err.message);
|
self.emit('error', err.message);
|
||||||
});
|
});
|
||||||
@@ -463,7 +471,7 @@ MQTT.prototype.disconnect = function () {
|
|||||||
retain: bool // the server should retain this message and send it out again to new subscribers
|
retain: bool // the server should retain this message and send it out again to new subscribers
|
||||||
dup : bool // indicate the message is a duplicate because original wasn't ACKed (QoS > 0 only)
|
dup : bool // indicate the message is a duplicate because original wasn't ACKed (QoS > 0 only)
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
MQTT.prototype.publish = function (topic, message, opts) {
|
MQTT.prototype.publish = function (topic, message, opts) {
|
||||||
if (!this.client) return;
|
if (!this.client) return;
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
@@ -577,34 +585,50 @@ MQTT.prototype.mqttConnect = function (clean) {
|
|||||||
/** This is 'exported' so it can be used with `require('MQTT.js').create(server, options)` */
|
/** This is 'exported' so it can be used with `require('MQTT.js').create(server, options)` */
|
||||||
export function create(server, options): MQTT {
|
export function create(server, options): MQTT {
|
||||||
return new MQTT(server, options);
|
return new MQTT(server, options);
|
||||||
};
|
}
|
||||||
|
|
||||||
export function connect(options): MQTT {
|
export function connect(options): MQTT {
|
||||||
var mqtt = new MQTT(options.host, options);
|
var mqtt = new MQTT(options.host, options);
|
||||||
mqtt.connect();
|
mqtt.connect();
|
||||||
return mqtt;
|
return mqtt;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
export interface MQTT {
|
export interface MQTT {
|
||||||
connect(): void;
|
connect(): void;
|
||||||
|
|
||||||
publish(topic: string, message: string, opts: {
|
publish(topic: string, message: string, opts: {
|
||||||
retain?: boolean, // the server should retain this message and send it out again to new subscribers
|
retain?: boolean, // the server should retain this message and send it out again to new subscribers
|
||||||
dup?: boolean, // indicate the message is a duplicate because original wasn't ACKed (QoS > 0 only)
|
dup?: boolean, // indicate the message is a duplicate because original wasn't ACKed (QoS > 0 only)
|
||||||
} | undefined): void;
|
} | undefined): void;
|
||||||
|
|
||||||
subscribe(topics: string[]): void;
|
subscribe(topics: string[]): void;
|
||||||
|
|
||||||
unsubscribe(topic: string): void;
|
unsubscribe(topic: string): void;
|
||||||
|
|
||||||
on(ev: 'publish', cb: (data: { topic: Buffer, message: Buffer }) => void): void;
|
on(ev: 'publish', cb: (data: { topic: Buffer, message: Buffer }) => void): void;
|
||||||
|
|
||||||
on(ev: 'message', cb: (topic: string, message: string) => void): void;
|
on(ev: 'message', cb: (topic: string, message: string) => void): void;
|
||||||
|
|
||||||
on(ev: 'puback', cb: (data) => void): void;
|
on(ev: 'puback', cb: (data) => void): void;
|
||||||
|
|
||||||
on(ev: 'pubcomp', cb: (data) => void): void;
|
on(ev: 'pubcomp', cb: (data) => void): void;
|
||||||
|
|
||||||
on(ev: 'subscribed_fail', cb: (data) => void): void;
|
on(ev: 'subscribed_fail', cb: (data) => void): void;
|
||||||
|
|
||||||
on(ev: 'subscribed', cb: (data) => void): void;
|
on(ev: 'subscribed', cb: (data) => void): void;
|
||||||
|
|
||||||
on(ev: 'unsubscribed', cb: (data) => void): void;
|
on(ev: 'unsubscribed', cb: (data) => void): void;
|
||||||
|
|
||||||
on(ev: 'ping_reply', cb: (data) => void): void;
|
on(ev: 'ping_reply', cb: (data) => void): void;
|
||||||
|
|
||||||
on(ev: 'connected', cb: (data) => void): void;
|
on(ev: 'connected', cb: (data) => void): void;
|
||||||
|
|
||||||
on(ev: 'connect', cb: (data) => void): void;
|
on(ev: 'connect', cb: (data) => void): void;
|
||||||
|
|
||||||
on(ev: 'error', cb: (data) => void): void;
|
on(ev: 'error', cb: (data) => void): void;
|
||||||
|
|
||||||
on(ev: 'disconnected', cb: (data) => void): void;
|
on(ev: 'disconnected', cb: (data) => void): void;
|
||||||
|
|
||||||
on(ev: 'close', cb: (data) => void): void;
|
on(ev: 'close', cb: (data) => void): void;
|
||||||
};
|
}
|
||||||
Reference in new issue
Block a user