Complete docs
This commit is contained in:
1 parent
2a1efb2aff
commit
edb64063ba
11 files changed
+155
-37
No files matched your search
+25
-9
@@ -2,6 +2,9 @@ import { HAComponent, HADevice, HAmqtt } from "./HAmqtt";
|
||||
import { makeLogger } from "./Logger";
|
||||
import { Button, ButtonModule } from "./flicTypes";
|
||||
|
||||
const ms2ISO8601 = (ms: number): string =>
|
||||
(new Date(ms)).toISOString().slice(0, 19).replace('T', ' ')
|
||||
|
||||
export type ButtonControllerOpt = {
|
||||
debug: boolean;
|
||||
}
|
||||
@@ -10,11 +13,14 @@ export const makeOptions = (opt: Partial<ButtonControllerOpt>): ButtonController
|
||||
...opt,
|
||||
})
|
||||
const ENTITIES: Record<string, [HAComponent, Record<string, any>]> = {
|
||||
"action": ['sensor', { icon: 'mdi:gesture-tap-button' }],
|
||||
"action": ['sensor', { icon: 'mdi:gesture-tap-button', name: "Click Action" }],
|
||||
"state": ['sensor', { icon: 'mdi:radiobox-indeterminate-variant' }],
|
||||
"battery": ['sensor', { unit_of_measurement: '%', device_class: 'battery' }],
|
||||
"connected": ['binary_sensor', { device_class: 'connectivity' }],
|
||||
"ready": ['binary_sensor', { device_class: 'running' }],
|
||||
"batteryLastUpdate": ['sensor', { name: "Battery Last Update Time", device_class: "duration", unit_of_measurement: "s" }],
|
||||
"connected": ['binary_sensor', { device_class: 'connectivity', name: "Connection Established" }],
|
||||
"ready": ['binary_sensor', { device_class: 'connectivity', name: "Connection Verified" }],
|
||||
"activeDisconnect": ['binary_sensor', { name: "User Active Disconnect" }],
|
||||
"passive": ['binary_sensor', { name: "Passive Mode" }],
|
||||
}
|
||||
export type ButtonController = ReturnType<typeof makeButtonController>;
|
||||
export function makeButtonController(
|
||||
@@ -55,11 +61,6 @@ export function makeButtonController(
|
||||
payload_not_available: 'OFF',
|
||||
topic: ha.genFlicPrefix(genButtonUniqueId(button.bdaddr), 'ready')
|
||||
},
|
||||
{
|
||||
payload_available: 'ON',
|
||||
payload_not_available: 'OFF',
|
||||
topic: ha.genFlicPrefix(genButtonUniqueId(button.bdaddr), 'connected')
|
||||
}
|
||||
],
|
||||
availability_mode: 'all',
|
||||
},
|
||||
@@ -84,8 +85,11 @@ export function makeButtonController(
|
||||
}
|
||||
const publishButtonMeta = (button: Button) => {
|
||||
ha.publishState(genButtonUniqueId(button.bdaddr), 'battery', button.batteryStatus);
|
||||
ha.publishState(genButtonUniqueId(button.bdaddr), 'batteryLastUpdate', button.batteryTimestamp ? `${Math.round((Date.now() - button.batteryTimestamp) / 1000)}` : 'unknown');
|
||||
ha.publishState(genButtonUniqueId(button.bdaddr), 'connected', button.connected ? 'ON' : "OFF");
|
||||
ha.publishState(genButtonUniqueId(button.bdaddr), 'ready', button.ready ? 'ON' : "OFF");
|
||||
ha.publishState(genButtonUniqueId(button.bdaddr), 'activeDisconnect', button.activeDisconnect ? 'ON' : "OFF");
|
||||
ha.publishState(genButtonUniqueId(button.bdaddr), 'passive', button.activeDisconnect ? 'ON' : "OFF");
|
||||
}
|
||||
const addBtn = (eventName: string) => (obj: { bdaddr: string }) => {
|
||||
const button = buttonModule.getButton(obj.bdaddr);
|
||||
@@ -94,9 +98,15 @@ export function makeButtonController(
|
||||
}
|
||||
const start = () => {
|
||||
logger.info("Starting...")
|
||||
let resetActiontInv: null | any = null;
|
||||
buttonModule.on('buttonAdded', addBtn('buttonAdded'));
|
||||
buttonModule.on('buttonConnected', addBtn('buttonConnected'));
|
||||
buttonModule.on('buttonReady', addBtn('buttonReady'));
|
||||
buttonModule.on('buttonReady', (btn) => {
|
||||
addBtn('buttonReady')(btn);
|
||||
const button = buttonModule.getButton(btn.bdaddr);
|
||||
publishButtonState(button, 'released')
|
||||
publishButtonAction(button, 'none')
|
||||
});
|
||||
buttonModule.on('buttonUpdated', addBtn('buttonUpdated'));
|
||||
buttonModule.on('buttonDeleted', (btn) => {
|
||||
logger.debug('buttonDeleted', JSON.stringify(btn, null, 4))
|
||||
@@ -117,12 +127,18 @@ export function makeButtonController(
|
||||
publishButtonMeta(btn);
|
||||
});
|
||||
buttonModule.on('buttonSingleOrDoubleClickOrHold', obj => {
|
||||
if (resetActiontInv !== null) {
|
||||
clearTimeout(resetActiontInv);
|
||||
}
|
||||
const btn = buttonModule.getButton(obj.bdaddr);
|
||||
publishButtonAction(
|
||||
btn,
|
||||
obj.isSingleClick ? "click" : obj.isDoubleClick ? "double_click" : "hold",
|
||||
);
|
||||
publishButtonMeta(btn);
|
||||
resetActiontInv = setTimeout(() => {
|
||||
publishButtonAction(btn, 'none');
|
||||
}, 500)
|
||||
});
|
||||
logger.info("Registering all buttons...")
|
||||
buttonModule.getButtons().forEach(registerButton);
|
||||
|
||||
+1
-1
@@ -62,8 +62,8 @@ export function makeHAmqtt(
|
||||
) => {
|
||||
const configtopic = genHAPrefix(component, nodeId, objectId) + "/config"
|
||||
const configObj = {
|
||||
...additionalProps,
|
||||
name,
|
||||
...additionalProps,
|
||||
state_topic: genFlicPrefix(nodeId, objectId),
|
||||
unique_id: `Flic_${nodeId}_${objectId}`,
|
||||
device,
|
||||
|
||||
+4
-3
@@ -16,17 +16,18 @@ const convertStr2Uint32Array = (s: string): Uint32Array => {
|
||||
return new Uint32Array(a.slice(0, a.length - 2).map(v => parseInt(v, 32)))
|
||||
}
|
||||
export type IRControllerOpt = {
|
||||
uniqueId: string,
|
||||
debug: boolean;
|
||||
}
|
||||
export const makeOptions = (opt: Partial<IRControllerOpt>): IRControllerOpt => ({
|
||||
debug: false,
|
||||
uniqueId: "0",
|
||||
...opt,
|
||||
})
|
||||
export const makeIRController = (
|
||||
ir: IRModule,
|
||||
ha: HAmqtt,
|
||||
mqtt: MQTT,
|
||||
uniqueId: string,
|
||||
options: Partial<IRControllerOpt> = {},
|
||||
) => {
|
||||
options = makeOptions(options);
|
||||
@@ -34,10 +35,10 @@ export const makeIRController = (
|
||||
const haDevice: HADevice = {
|
||||
name: 'IR',
|
||||
manufacturer: 'Flic',
|
||||
model: `${NODE_ID}${uniqueId}`,
|
||||
model: `${NODE_ID}${options.uniqueId}`,
|
||||
identifiers: ['FlicHubIR']
|
||||
}
|
||||
const nodeId = `${NODE_ID}${uniqueId}`;
|
||||
const nodeId = `${NODE_ID}${options.uniqueId}`;
|
||||
const RECORD_SIGNAL_SET = ha.genFlicPrefix(nodeId, 'record/set');
|
||||
const VALUE_SIGNAL_SET = ha.genFlicPrefix(nodeId, 'signal/set');
|
||||
const VALUE_SIGNAL_STATE = ha.genFlicPrefix(nodeId, 'signal');
|
||||
|
||||
@@ -9,6 +9,7 @@ export type Button = {
|
||||
connected: boolean // The connection to the button is currently established
|
||||
ready: boolean // The connection is verified (see buttonReady)
|
||||
batteryStatus: number | null // Battery level in percent (0-100), or null if unknown
|
||||
batteryTimestamp: number | null // Last time the battery was updated
|
||||
firmwareVersion: number | null // Firmware version of button, or null if unknown
|
||||
flicVersion: number | null // Flic version (1 or 2)
|
||||
uuid: string // A 32 characters long hex string, unique for every button
|
||||
|
||||
+2
-2
@@ -23,7 +23,6 @@ export type Options = {
|
||||
export const start = (
|
||||
buttonModule: ButtonModule,
|
||||
irModule: IRModule,
|
||||
uniqueId: string,
|
||||
options: Options,
|
||||
) => {
|
||||
const mqttServer = mqtt.create(
|
||||
@@ -33,8 +32,10 @@ export const start = (
|
||||
const logger = makeLogger('root', options.debug ?? false);
|
||||
options.ha = options.ha ?? {}
|
||||
options.flicBtns = options.flicBtns ?? {}
|
||||
options.flicIR = options.flicIR ?? {}
|
||||
options.ha.debug = options.ha.debug ?? options.debug ?? false
|
||||
options.flicBtns.debug = options.flicBtns.debug ?? options.debug ?? false
|
||||
options.flicIR.debug = options.flicBtns.debug ?? options.debug ?? false
|
||||
const ha = makeHAmqtt(mqttServer, options.ha);
|
||||
mqttServer.on('connected', () => {
|
||||
logger.info("connected to mqtt")
|
||||
@@ -48,7 +49,6 @@ export const start = (
|
||||
irModule,
|
||||
ha,
|
||||
mqttServer,
|
||||
uniqueId,
|
||||
options.flicIR,
|
||||
).start();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user