All the work
This commit is contained in:
1 parent
f6767c9e8f
commit
2a1efb2aff
15 files changed
+5006
-349
No files matched your search
@@ -0,0 +1,139 @@
|
||||
import { HAComponent, HADevice, HAmqtt } from "./HAmqtt";
|
||||
import { makeLogger } from "./Logger";
|
||||
import { Button, ButtonModule } from "./flicTypes";
|
||||
|
||||
export type ButtonControllerOpt = {
|
||||
debug: boolean;
|
||||
}
|
||||
export const makeOptions = (opt: Partial<ButtonControllerOpt>): ButtonControllerOpt => ({
|
||||
debug: false,
|
||||
...opt,
|
||||
})
|
||||
const ENTITIES: Record<string, [HAComponent, Record<string, any>]> = {
|
||||
"action": ['sensor', { icon: 'mdi:gesture-tap-button' }],
|
||||
"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' }],
|
||||
}
|
||||
export type ButtonController = ReturnType<typeof makeButtonController>;
|
||||
export function makeButtonController(
|
||||
ha: HAmqtt,
|
||||
buttonModule: ButtonModule,
|
||||
options: Partial<ButtonControllerOpt> = {},
|
||||
) {
|
||||
options = makeOptions(options);
|
||||
const logger = makeLogger('btnc', options.debug)
|
||||
logger.info("Starting Flic ButtonController with", JSON.stringify(options, null, 4));
|
||||
|
||||
const getDeviceFromButton = (button: Button): HADevice => {
|
||||
return {
|
||||
name: button.name,
|
||||
identifiers: [button.serialNumber, button.uuid],
|
||||
manufacturer: 'Flic',
|
||||
model: `v${button.flicVersion}_${button.color.trim().length > 0 ? button.color : 'white'}`,
|
||||
sw: String(button.firmwareVersion),
|
||||
hw: String(button.flicVersion),
|
||||
}
|
||||
}
|
||||
const genButtonUniqueId = (bdaddr: string): string => bdaddr.replace(/:/g, '_')
|
||||
const registerButton = (button: Button) => {
|
||||
logger.info('Registering', JSON.stringify(button, null, 4))
|
||||
const haDevice = getDeviceFromButton(button);
|
||||
Object.keys(ENTITIES).forEach(objectId => {
|
||||
ha.registerEntity(
|
||||
`Button ${objectId}`,
|
||||
ENTITIES[objectId][0],
|
||||
genButtonUniqueId(button.bdaddr),
|
||||
objectId,
|
||||
haDevice,
|
||||
{
|
||||
...ENTITIES[objectId][1],
|
||||
availability: [
|
||||
{
|
||||
payload_available: 'ON',
|
||||
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',
|
||||
},
|
||||
)
|
||||
});
|
||||
}
|
||||
const deregisterButton = (button: Button) => {
|
||||
logger.info('Deregistering', JSON.stringify(button, null, 4))
|
||||
Object.keys(ENTITIES).forEach(objectId => {
|
||||
ha.deregisterEntity(
|
||||
ENTITIES[objectId][0],
|
||||
genButtonUniqueId(button.bdaddr),
|
||||
objectId,
|
||||
)
|
||||
});
|
||||
}
|
||||
const publishButtonState = (button: Button, state: 'released' | 'pressed') => {
|
||||
ha.publishState(genButtonUniqueId(button.bdaddr), 'state', state);
|
||||
}
|
||||
const publishButtonAction = (button: Button, state: string) => {
|
||||
ha.publishState(genButtonUniqueId(button.bdaddr), 'action', state);
|
||||
}
|
||||
const publishButtonMeta = (button: Button) => {
|
||||
ha.publishState(genButtonUniqueId(button.bdaddr), 'battery', button.batteryStatus);
|
||||
ha.publishState(genButtonUniqueId(button.bdaddr), 'connected', button.connected ? 'ON' : "OFF");
|
||||
ha.publishState(genButtonUniqueId(button.bdaddr), 'ready', button.ready ? 'ON' : "OFF");
|
||||
}
|
||||
const addBtn = (eventName: string) => (obj: { bdaddr: string }) => {
|
||||
const button = buttonModule.getButton(obj.bdaddr);
|
||||
logger.info(eventName, "upserting", button.name, genButtonUniqueId(button.bdaddr));
|
||||
registerButton(button);
|
||||
}
|
||||
const start = () => {
|
||||
logger.info("Starting...")
|
||||
buttonModule.on('buttonAdded', addBtn('buttonAdded'));
|
||||
buttonModule.on('buttonConnected', addBtn('buttonConnected'));
|
||||
buttonModule.on('buttonReady', addBtn('buttonReady'));
|
||||
buttonModule.on('buttonUpdated', addBtn('buttonUpdated'));
|
||||
buttonModule.on('buttonDeleted', (btn) => {
|
||||
logger.debug('buttonDeleted', JSON.stringify(btn, null, 4))
|
||||
deregisterButton(btn);
|
||||
publishButtonMeta(btn);
|
||||
});
|
||||
buttonModule.on('buttonDisconnected', ({ bdaddr }) => {
|
||||
publishButtonMeta(buttonModule.getButton(bdaddr));
|
||||
});
|
||||
buttonModule.on('buttonDown', ({ bdaddr }) => {
|
||||
const btn = buttonModule.getButton(bdaddr);
|
||||
publishButtonState(btn, 'pressed');
|
||||
publishButtonMeta(btn);
|
||||
});
|
||||
buttonModule.on('buttonUp', ({ bdaddr }) => {
|
||||
const btn = buttonModule.getButton(bdaddr);
|
||||
publishButtonState(btn, 'released');
|
||||
publishButtonMeta(btn);
|
||||
});
|
||||
buttonModule.on('buttonSingleOrDoubleClickOrHold', obj => {
|
||||
const btn = buttonModule.getButton(obj.bdaddr);
|
||||
publishButtonAction(
|
||||
btn,
|
||||
obj.isSingleClick ? "click" : obj.isDoubleClick ? "double_click" : "hold",
|
||||
);
|
||||
publishButtonMeta(btn);
|
||||
});
|
||||
logger.info("Registering all buttons...")
|
||||
buttonModule.getButtons().forEach(registerButton);
|
||||
setInterval(() => buttonModule.getButtons().forEach(publishButtonMeta), 1000);
|
||||
logger.info('is up')
|
||||
}
|
||||
|
||||
return {
|
||||
start,
|
||||
publishButtonAction,
|
||||
publishButtonMeta,
|
||||
publishButtonState,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user