Complete docs

This commit is contained in:
Ariel Sosnovsky committed 2023-11-02 10:43:39 -04:00
1 parent 2a1efb2aff
commit edb64063ba
11 files changed
+155 -37

No files matched your search

+25 -9
View File
@@ -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);