stable place
This commit is contained in:
1 parent
dc0ce5ba41
commit
03dda555a0
14 files changed
+956
-735
No files matched your search
@@ -0,0 +1,162 @@
|
||||
import buttonModule, { Button } from "buttons";
|
||||
import { Logger } from "../Logger";
|
||||
import {
|
||||
ButtonConstants,
|
||||
deregisterButton,
|
||||
genButtonUniqueId,
|
||||
getConstants,
|
||||
registerEntities,
|
||||
} from "./utils";
|
||||
import { HAmqtt } from "../ha_mqtt/index";
|
||||
|
||||
export const createHandleBtnCreation =
|
||||
(ha: HAmqtt, logger: Logger) =>
|
||||
(eventName: string) =>
|
||||
(button: Button): ButtonConstants => {
|
||||
logger.info(
|
||||
eventName,
|
||||
"upserting",
|
||||
button.name,
|
||||
genButtonUniqueId(button.bdaddr),
|
||||
);
|
||||
const constants = registerEntities(ha, button, logger);
|
||||
publishButtonMeta(ha, button, constants, logger);
|
||||
return constants;
|
||||
};
|
||||
|
||||
const publishButtonMeta = (
|
||||
ha: HAmqtt,
|
||||
button: Button,
|
||||
constants: ButtonConstants,
|
||||
logger: Logger,
|
||||
): ButtonConstants => {
|
||||
logger.debug(`Publishing Button Metadata ${JSON.stringify(button)}`);
|
||||
ha.publishStateFromObject(constants.states.BATTERY, button.batteryStatus);
|
||||
ha.publishStateFromObject(
|
||||
constants.signals.CONNECTED,
|
||||
button.connected ? "ON" : "OFF",
|
||||
);
|
||||
ha.publishStateFromObject(
|
||||
constants.signals.READY,
|
||||
button.ready ? "ON" : "OFF",
|
||||
);
|
||||
ha.publishStateFromObject(
|
||||
constants.states.ACTIVE_DISCONNECT,
|
||||
button.activeDisconnect ? "ON" : "OFF",
|
||||
);
|
||||
return constants;
|
||||
};
|
||||
|
||||
export const publishButtonMetaByBdaddr = (
|
||||
ha: HAmqtt,
|
||||
bdaddr: string,
|
||||
logger: Logger,
|
||||
): ButtonConstants | null => {
|
||||
const button = buttonModule.getButton(bdaddr);
|
||||
if (!button) {
|
||||
logger.error(`Cannot publish metadata for ${{ bdaddr }}`);
|
||||
return null;
|
||||
}
|
||||
const constants = getConstants(ha, bdaddr);
|
||||
return publishButtonMeta(ha, button, constants, logger);
|
||||
};
|
||||
|
||||
const publishButtonAction = (
|
||||
ha: HAmqtt,
|
||||
logger: Logger,
|
||||
constants: ButtonConstants,
|
||||
state: "click" | "double_click" | "hold" | "none",
|
||||
) => {
|
||||
ha.publishStateFromObject(constants.states.ACTION, state);
|
||||
logger.debug(
|
||||
`Publishing click for uniqId="${constants.uniqId}" state=${state}`,
|
||||
);
|
||||
if (state === "click") {
|
||||
ha.publishStateFromObject(
|
||||
constants.actions.SHORT_PRESS,
|
||||
constants.actions.SHORT_PRESS.objectId,
|
||||
);
|
||||
} else if (state === "double_click") {
|
||||
ha.publishStateFromObject(
|
||||
constants.actions.DOUBLE_PRESS,
|
||||
constants.actions.DOUBLE_PRESS.objectId,
|
||||
);
|
||||
} else if (state === "hold") {
|
||||
ha.publishStateFromObject(
|
||||
constants.actions.LONG_PRESS,
|
||||
constants.actions.LONG_PRESS.objectId,
|
||||
);
|
||||
}
|
||||
};
|
||||
export const setListeners = (ha: HAmqtt, logger: Logger) => {
|
||||
const resetActiontInv: Record<string, any> = {};
|
||||
const handleButtonCreation = createHandleBtnCreation(ha, logger);
|
||||
buttonModule.on("buttonAdded", (btn) =>
|
||||
handleButtonCreation("buttonAdded")(btn.button),
|
||||
);
|
||||
buttonModule.on("buttonUpdated", (btn) =>
|
||||
handleButtonCreation("buttonUpdated")(btn.button),
|
||||
);
|
||||
buttonModule.on("buttonDeleted", ({ bdaddr }) => {
|
||||
deregisterButton(ha, bdaddr, logger);
|
||||
if (resetActiontInv[bdaddr]) {
|
||||
clearTimeout(resetActiontInv[bdaddr]);
|
||||
resetActiontInv[bdaddr] = null;
|
||||
}
|
||||
});
|
||||
buttonModule.on("buttonConnected", ({ bdaddr }) => {
|
||||
const btn = buttonModule.getButton(bdaddr);
|
||||
if (btn) {
|
||||
handleButtonCreation("buttonConnected")(btn);
|
||||
}
|
||||
});
|
||||
buttonModule.on("buttonReady", ({ bdaddr }) => {
|
||||
const btn = buttonModule.getButton(bdaddr);
|
||||
if (btn) {
|
||||
const constants = handleButtonCreation("buttonReady")(btn);
|
||||
ha.publishStateFromObject(constants.states.STATE, "released");
|
||||
publishButtonAction(ha, logger, constants, "none");
|
||||
}
|
||||
});
|
||||
buttonModule.on("buttonDisconnected", ({ bdaddr }) => {
|
||||
if (resetActiontInv[bdaddr]) {
|
||||
clearTimeout(resetActiontInv[bdaddr]);
|
||||
resetActiontInv[bdaddr] = null;
|
||||
}
|
||||
publishButtonMetaByBdaddr(ha, bdaddr, logger);
|
||||
});
|
||||
buttonModule.on("buttonDown", ({ bdaddr }) => {
|
||||
const constants = publishButtonMetaByBdaddr(ha, bdaddr, logger);
|
||||
if (constants) {
|
||||
ha.publishStateFromObject(constants.states.STATE, "pressed");
|
||||
}
|
||||
});
|
||||
buttonModule.on("buttonUp", ({ bdaddr }) => {
|
||||
const constants = publishButtonMetaByBdaddr(ha, bdaddr, logger);
|
||||
if (constants) {
|
||||
ha.publishStateFromObject(constants.states.STATE, "released");
|
||||
}
|
||||
});
|
||||
buttonModule.on("buttonSingleOrDoubleClickOrHold", (obj) => {
|
||||
const constants = publishButtonMetaByBdaddr(ha, obj.bdaddr, logger);
|
||||
if (constants) {
|
||||
if (resetActiontInv[obj.bdaddr]) {
|
||||
clearTimeout(resetActiontInv[obj.bdaddr]);
|
||||
resetActiontInv[obj.bdaddr] = null;
|
||||
}
|
||||
publishButtonAction(
|
||||
ha,
|
||||
logger,
|
||||
constants,
|
||||
obj.isSingleClick
|
||||
? "click"
|
||||
: obj.isDoubleClick
|
||||
? "double_click"
|
||||
: "hold",
|
||||
);
|
||||
resetActiontInv[obj.bdaddr] = setTimeout(() => {
|
||||
publishButtonAction(ha, logger, constants, "none");
|
||||
}, 750);
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -1,118 +0,0 @@
|
||||
import buttonModule, { Button } from "buttons";
|
||||
import { HAmqtt } from "../ha_mqtt/index";
|
||||
import { Logger } from "../Logger";
|
||||
import { genButtonUniqueId, getDeviceFromButton } from "./utils";
|
||||
import { ENTITIES } from "./entities";
|
||||
|
||||
export const ButtonStateHandler = (ha: HAmqtt, logger: Logger) => {
|
||||
const registerButton = (button: Button) => {
|
||||
logger.info("Registering", JSON.stringify(button, null, 4));
|
||||
const haDevice = getDeviceFromButton(button);
|
||||
const uniqId = genButtonUniqueId(button.bdaddr);
|
||||
ha.startLifeLine("Button Controller Connected", uniqId, haDevice);
|
||||
Object.keys(ENTITIES).forEach((objectId) => {
|
||||
let avl: any = {
|
||||
availability: [
|
||||
{
|
||||
payload_available: "ON",
|
||||
payload_not_available: "unavailable",
|
||||
topic: ha.genFlicPrefix(genButtonUniqueId(button.bdaddr), "ready"),
|
||||
},
|
||||
{
|
||||
payload_available: "ON",
|
||||
payload_not_available: "unavailable",
|
||||
topic: ha.genFlicPrefix(
|
||||
genButtonUniqueId(button.bdaddr),
|
||||
"lifeline",
|
||||
),
|
||||
},
|
||||
],
|
||||
availability_mode: "all",
|
||||
};
|
||||
if (objectId === "ready" || objectId == "connected") {
|
||||
avl.availability = [avl.availability[1]];
|
||||
}
|
||||
if (ENTITIES[objectId][0] === "device_automation") {
|
||||
avl = {};
|
||||
}
|
||||
ha.registerEntity(
|
||||
`Button ${objectId}`,
|
||||
ENTITIES[objectId][0],
|
||||
uniqId,
|
||||
objectId,
|
||||
haDevice,
|
||||
{
|
||||
...ENTITIES[objectId][1],
|
||||
...avl,
|
||||
},
|
||||
);
|
||||
});
|
||||
};
|
||||
const deregisterButton = (bdaddr: string) => {
|
||||
const uniqId = genButtonUniqueId(bdaddr);
|
||||
logger.info("Deregistering", JSON.stringify({ bdaddr, uniqId }, null, 4));
|
||||
Object.keys(ENTITIES).forEach((objectId) => {
|
||||
ha.deregisterEntity(ENTITIES[objectId][0], uniqId, objectId);
|
||||
});
|
||||
};
|
||||
const publishButtonState = (
|
||||
bdaddr: string,
|
||||
state: "released" | "pressed",
|
||||
) => {
|
||||
logger.debug(`Updating state for bdaddr="${bdaddr}" state=${state}`);
|
||||
ha.publishState(genButtonUniqueId(bdaddr), "state", state);
|
||||
};
|
||||
const publishButtonAction = (
|
||||
bdaddr: string,
|
||||
state: "click" | "double_click" | "hold" | "none",
|
||||
) => {
|
||||
const uniqId = genButtonUniqueId(bdaddr);
|
||||
ha.publishState(uniqId, "action", state);
|
||||
logger.debug(
|
||||
`Publishing click for bdaddr="${bdaddr}" uniqId="${uniqId}" state=${state}`,
|
||||
);
|
||||
if (state === "click") {
|
||||
ha.publishState(uniqId, "button_short_press", "ON");
|
||||
} else if (state === "double_click") {
|
||||
ha.publishState(uniqId, "button_double_press", "ON");
|
||||
} else if (state === "hold") {
|
||||
ha.publishState(uniqId, "button_long_press", "ON");
|
||||
}
|
||||
};
|
||||
const publishButtonMeta = (bdaddr: string) => {
|
||||
const button = buttonModule.getButton(bdaddr);
|
||||
const uniqId = genButtonUniqueId(button.bdaddr);
|
||||
ha.publishState(uniqId, "name", button.name);
|
||||
ha.publishState(uniqId, "battery", button.batteryStatus);
|
||||
ha.publishState(uniqId, "connected", button.connected ? "ON" : "OFF");
|
||||
ha.publishState(uniqId, "ready", button.ready ? "ON" : "OFF");
|
||||
ha.publishState(
|
||||
uniqId,
|
||||
"activeDisconnect",
|
||||
button.activeDisconnect ? "ON" : "OFF",
|
||||
);
|
||||
ha.publishState(uniqId, "passive", button.activeDisconnect ? "ON" : "OFF");
|
||||
ha.publishState(uniqId, "lifeline", "ON");
|
||||
};
|
||||
const handleBtnCreation = (eventName: string, obj: { bdaddr: string }) => {
|
||||
const button = buttonModule.getButton(obj.bdaddr);
|
||||
logger.info(
|
||||
eventName,
|
||||
"upserting",
|
||||
button.name,
|
||||
genButtonUniqueId(button.bdaddr),
|
||||
);
|
||||
registerButton(button);
|
||||
};
|
||||
return {
|
||||
addBtn: (eventName: string) => (o: { bdaddr: string }) =>
|
||||
handleBtnCreation(eventName, o),
|
||||
addBtnWithObject: (eventName: string) => (o: { button: Button }) =>
|
||||
handleBtnCreation(eventName, o.button),
|
||||
publishButtonMeta,
|
||||
publishButtonAction,
|
||||
publishButtonState,
|
||||
registerButton,
|
||||
deregisterButton,
|
||||
};
|
||||
};
|
||||
@@ -1,73 +0,0 @@
|
||||
export const ENTITIES: Record<string, [HAComponent, Record<string, any>]> = {
|
||||
name: [
|
||||
"sensor",
|
||||
{
|
||||
entity_category: "diagnostic",
|
||||
name: "Button Name",
|
||||
},
|
||||
],
|
||||
action: ["sensor", { icon: "mdi:gesture-tap-button", name: "Click Action" }],
|
||||
state: ["sensor", { icon: "mdi:radiobox-indeterminate-variant" }],
|
||||
battery: [
|
||||
"sensor",
|
||||
{ expire_after: 5, unit_of_measurement: "%", device_class: "battery" },
|
||||
],
|
||||
connected: [
|
||||
"binary_sensor",
|
||||
{
|
||||
entity_category: "diagnostic",
|
||||
expire_after: 5,
|
||||
device_class: "connectivity",
|
||||
name: "Connection Established",
|
||||
payload_not_available: "OFF",
|
||||
},
|
||||
],
|
||||
ready: [
|
||||
"binary_sensor",
|
||||
{
|
||||
entity_category: "config",
|
||||
expire_after: 5,
|
||||
device_class: "connectivity",
|
||||
name: "Connection Verified",
|
||||
},
|
||||
],
|
||||
activeDisconnect: [
|
||||
"binary_sensor",
|
||||
{
|
||||
entity_category: "config",
|
||||
expire_after: 5,
|
||||
name: "User Active Disconnect",
|
||||
},
|
||||
],
|
||||
passive: [
|
||||
"binary_sensor",
|
||||
{ entity_category: "config", expire_after: 5, name: "Passive Mode" },
|
||||
],
|
||||
button_short_press: [
|
||||
"device_automation",
|
||||
{
|
||||
type: "button_short_press",
|
||||
subtype: "button_1",
|
||||
automation_type: "trigger",
|
||||
payload: "ON",
|
||||
},
|
||||
],
|
||||
button_long_press: [
|
||||
"device_automation",
|
||||
{
|
||||
type: "button_long_press",
|
||||
subtype: "button_1",
|
||||
automation_type: "trigger",
|
||||
payload: "ON",
|
||||
},
|
||||
],
|
||||
button_double_press: [
|
||||
"device_automation",
|
||||
{
|
||||
type: "button_double_press",
|
||||
subtype: "button_1",
|
||||
automation_type: "trigger",
|
||||
payload: "ON",
|
||||
},
|
||||
],
|
||||
};
|
||||
@@ -1,93 +1,33 @@
|
||||
import buttonModule from "buttons";
|
||||
import { HAmqtt } from "../ha_mqtt/index";
|
||||
import { makeLogger } from "../Logger";
|
||||
import { makeOptions } from "./options";
|
||||
import { ButtonStateHandler } from "./button_state_handler";
|
||||
import { HAmqtt } from "../ha_mqtt/index";
|
||||
import {
|
||||
createHandleBtnCreation,
|
||||
publishButtonMetaByBdaddr,
|
||||
setListeners,
|
||||
} from "./button_module_handler";
|
||||
import { makeOptions } from "./utils";
|
||||
import buttonModule from "buttons";
|
||||
|
||||
export type ButtonController = ReturnType<typeof makeButtonController>;
|
||||
export function makeButtonController(
|
||||
export const startButtinStateHandler = (
|
||||
ha: HAmqtt,
|
||||
options: Partial<ButtonControllerOpt> = {},
|
||||
) {
|
||||
options = makeOptions(options);
|
||||
_options: Partial<ButtonControllerOpt> = {},
|
||||
) => {
|
||||
const options = makeOptions(_options);
|
||||
const logger = makeLogger("btnc", options.debug);
|
||||
logger.info(
|
||||
"Starting Flic ButtonController with",
|
||||
JSON.stringify(options, null, 4),
|
||||
);
|
||||
const stateHandler = ButtonStateHandler(ha, logger);
|
||||
const setListeners = () => {
|
||||
let resetActiontInv: null | any = null;
|
||||
buttonModule.on(
|
||||
"buttonAdded",
|
||||
stateHandler.addBtnWithObject("buttonAdded"),
|
||||
);
|
||||
buttonModule.on(
|
||||
"buttonUpdated",
|
||||
stateHandler.addBtnWithObject("buttonUpdated"),
|
||||
);
|
||||
buttonModule.on("buttonDeleted", (btn) => {
|
||||
logger.debug("buttonDeleted", JSON.stringify(btn, null, 4));
|
||||
stateHandler.deregisterButton(btn.bdaddr);
|
||||
stateHandler.publishButtonMeta(btn.bdaddr);
|
||||
});
|
||||
buttonModule.on("buttonConnected", stateHandler.addBtn("buttonConnected"));
|
||||
buttonModule.on("buttonReady", (btn) => {
|
||||
stateHandler.addBtn("buttonReady")(btn);
|
||||
stateHandler.publishButtonState(btn.bdaddr, "released");
|
||||
stateHandler.publishButtonAction(btn.bdaddr, "none");
|
||||
});
|
||||
buttonModule.on("buttonDisconnected", ({ bdaddr }) => {
|
||||
stateHandler.publishButtonMeta(bdaddr);
|
||||
});
|
||||
buttonModule.on("buttonDown", ({ bdaddr }) => {
|
||||
stateHandler.publishButtonState(bdaddr, "pressed");
|
||||
stateHandler.publishButtonMeta(bdaddr);
|
||||
});
|
||||
buttonModule.on("buttonUp", ({ bdaddr }) => {
|
||||
stateHandler.publishButtonState(bdaddr, "released");
|
||||
stateHandler.publishButtonMeta(bdaddr);
|
||||
});
|
||||
buttonModule.on("buttonClickOrHold", (obj) => {});
|
||||
buttonModule.on("buttonSingleOrDoubleClickOrHold", (obj) => {
|
||||
if (resetActiontInv !== null) {
|
||||
clearTimeout(resetActiontInv);
|
||||
}
|
||||
stateHandler.publishButtonAction(
|
||||
obj.bdaddr,
|
||||
obj.isSingleClick
|
||||
? "click"
|
||||
: obj.isDoubleClick
|
||||
? "double_click"
|
||||
: "hold",
|
||||
);
|
||||
stateHandler.publishButtonMeta(obj.bdaddr);
|
||||
resetActiontInv = setTimeout(() => {
|
||||
stateHandler.publishButtonAction(obj.bdaddr, "none");
|
||||
}, 500);
|
||||
});
|
||||
};
|
||||
const start = () => {
|
||||
logger.info("Starting...");
|
||||
logger.info("Setting listeners...");
|
||||
setListeners();
|
||||
logger.info("Registering all buttons...");
|
||||
logger.info("Setting listeners...");
|
||||
setListeners(ha, logger);
|
||||
logger.info("Registering all buttons...");
|
||||
const handleCreation = createHandleBtnCreation(ha, logger)("kickStart");
|
||||
buttonModule.getButtons().forEach(handleCreation);
|
||||
setInterval(() => {
|
||||
logger.debug("Updating button state!");
|
||||
buttonModule
|
||||
.getButtons()
|
||||
.forEach((btn) => stateHandler.registerButton(btn));
|
||||
setInterval(() => {
|
||||
logger.debug("Updating button state!");
|
||||
buttonModule
|
||||
.getButtons()
|
||||
.forEach((btn) => stateHandler.publishButtonMeta(btn.bdaddr));
|
||||
}, 3000);
|
||||
logger.info("is up");
|
||||
};
|
||||
|
||||
return {
|
||||
start,
|
||||
publishButtonAction: stateHandler.publishButtonAction,
|
||||
publishButtonMeta: stateHandler.publishButtonMeta,
|
||||
publishButtonState: stateHandler.publishButtonState,
|
||||
};
|
||||
}
|
||||
.forEach((btn) => publishButtonMetaByBdaddr(ha, btn.bdaddr, logger));
|
||||
}, 3000);
|
||||
logger.info("is up");
|
||||
};
|
||||
@@ -1,6 +0,0 @@
|
||||
export const makeOptions = (
|
||||
opt: Partial<ButtonControllerOpt>,
|
||||
): ButtonControllerOpt => ({
|
||||
debug: false,
|
||||
...opt,
|
||||
});
|
||||
@@ -1,5 +1,166 @@
|
||||
import { Button } from "buttons";
|
||||
import { HADevice } from "../ha_mqtt/index";
|
||||
import { HAmqtt } from "../ha_mqtt/index";
|
||||
import { Logger } from "../Logger";
|
||||
|
||||
export const makeOptions = (
|
||||
opt: Partial<ButtonControllerOpt>,
|
||||
): ButtonControllerOpt => ({
|
||||
debug: false,
|
||||
...opt,
|
||||
});
|
||||
|
||||
export const genButtonUniqueId = (bdaddr: string): string =>
|
||||
bdaddr.replace(/:/g, "_");
|
||||
|
||||
export type ButtonMqttEntity = FlicMqttPrefix & {
|
||||
component: HAComponent;
|
||||
additionalProps: Record<string, any>;
|
||||
};
|
||||
export type ButtonConstants = ReturnType<typeof getConstants>;
|
||||
export type ButtonConstantsNames = keyof ButtonConstants;
|
||||
export const getConstants = (ha: HAmqtt, bdaddr: string) => {
|
||||
const uniqId = genButtonUniqueId(bdaddr);
|
||||
const lifeline = ha.genFlicPrefixObject(uniqId, "lifeline");
|
||||
const ready = ha.genFlicPrefixObject(uniqId, "ready");
|
||||
const connected = ha.genFlicPrefixObject(uniqId, "connected");
|
||||
const key_availability = [
|
||||
{
|
||||
payload_available: "ON",
|
||||
payload_not_available: "unavailable",
|
||||
topic: lifeline.mqttPrefix,
|
||||
},
|
||||
];
|
||||
const availability = {
|
||||
availability: [
|
||||
...key_availability,
|
||||
{
|
||||
payload_available: "ON",
|
||||
payload_not_available: "unavailable",
|
||||
topic: ready.mqttPrefix,
|
||||
},
|
||||
{
|
||||
payload_available: "ON",
|
||||
payload_not_available: "unavailable",
|
||||
topic: connected.mqttPrefix,
|
||||
},
|
||||
],
|
||||
availability_mode: "all",
|
||||
};
|
||||
return {
|
||||
uniqId,
|
||||
LIFELINE_SIGNAL: {
|
||||
...lifeline,
|
||||
component: "binary_sensor",
|
||||
additionalProps: {
|
||||
entity_category: "diagnostic",
|
||||
expire_after: 5,
|
||||
device_class: "connectivity",
|
||||
name: "Button Controller Connected",
|
||||
payload_not_available: "OFF",
|
||||
},
|
||||
} as ButtonMqttEntity,
|
||||
|
||||
signals: {
|
||||
READY: {
|
||||
...ready,
|
||||
component: "binary_sensor",
|
||||
additionalProps: {
|
||||
entity_category: "diagnostic",
|
||||
expire_after: 5,
|
||||
device_class: "connectivity",
|
||||
name: "Button Ready",
|
||||
payload_not_available: "OFF",
|
||||
availability: key_availability,
|
||||
},
|
||||
} as ButtonMqttEntity,
|
||||
CONNECTED: {
|
||||
...connected,
|
||||
component: "binary_sensor",
|
||||
additionalProps: {
|
||||
entity_category: "diagnostic",
|
||||
expire_after: 5,
|
||||
device_class: "connectivity",
|
||||
name: "Button Connected",
|
||||
payload_not_available: "OFF",
|
||||
availability: key_availability,
|
||||
},
|
||||
} as ButtonMqttEntity,
|
||||
},
|
||||
|
||||
states: {
|
||||
ACTION: {
|
||||
...ha.genFlicPrefixObject(uniqId, "action"),
|
||||
component: "sensor",
|
||||
additionalProps: {
|
||||
icon: "mdi:gesture-tap-button",
|
||||
name: "Click Action",
|
||||
availability,
|
||||
},
|
||||
} as ButtonMqttEntity,
|
||||
STATE: {
|
||||
...ha.genFlicPrefixObject(uniqId, "state"),
|
||||
component: "sensor",
|
||||
additionalProps: {
|
||||
icon: "mdi:radiobox-indeterminate-variant",
|
||||
availability,
|
||||
},
|
||||
} as ButtonMqttEntity,
|
||||
BATTERY: {
|
||||
...ha.genFlicPrefixObject(uniqId, "battery"),
|
||||
component: "sensor",
|
||||
additionalProps: {
|
||||
expire_after: 5,
|
||||
unit_of_measurement: "%",
|
||||
device_class: "battery",
|
||||
availability,
|
||||
},
|
||||
} as ButtonMqttEntity,
|
||||
ACTIVE_DISCONNECT: {
|
||||
...ha.genFlicPrefixObject(uniqId, "activeDisconnect"),
|
||||
component: "binary_sensor",
|
||||
additionalProps: {
|
||||
entity_category: "config",
|
||||
expire_after: 5,
|
||||
name: "Explicitly disconnected button by user",
|
||||
availability,
|
||||
},
|
||||
} as ButtonMqttEntity,
|
||||
},
|
||||
|
||||
actions: {
|
||||
SHORT_PRESS: {
|
||||
...ha.genFlicPrefixObject(uniqId, "button_short_press"),
|
||||
component: "device_automation",
|
||||
additionalProps: {
|
||||
type: "button_short_press",
|
||||
subtype: "button_1",
|
||||
automation_type: "trigger",
|
||||
payload: "button_short_press",
|
||||
},
|
||||
} as ButtonMqttEntity,
|
||||
LONG_PRESS: {
|
||||
...ha.genFlicPrefixObject(uniqId, "button_long_press"),
|
||||
component: "device_automation",
|
||||
additionalProps: {
|
||||
type: "button_long_press",
|
||||
subtype: "button_1",
|
||||
automation_type: "trigger",
|
||||
payload: "button_long_press",
|
||||
},
|
||||
} as ButtonMqttEntity,
|
||||
DOUBLE_PRESS: {
|
||||
...ha.genFlicPrefixObject(uniqId, "button_double_press"),
|
||||
component: "device_automation",
|
||||
additionalProps: {
|
||||
type: "button_double_press",
|
||||
subtype: "button_1",
|
||||
automation_type: "trigger",
|
||||
payload: "button_double_press",
|
||||
},
|
||||
} as ButtonMqttEntity,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const getDeviceFromButton = (button: Button): HADevice => {
|
||||
return {
|
||||
@@ -13,5 +174,82 @@ export const getDeviceFromButton = (button: Button): HADevice => {
|
||||
configuration_url: "https://hubsdk.flic.io/",
|
||||
};
|
||||
};
|
||||
export const genButtonUniqueId = (bdaddr: string): string =>
|
||||
bdaddr.replace(/:/g, "_");
|
||||
export const registerEntities = (
|
||||
ha: HAmqtt,
|
||||
button: Button,
|
||||
logger: Logger,
|
||||
): ButtonConstants => {
|
||||
const constants = getConstants(ha, button.bdaddr);
|
||||
const haDevice = getDeviceFromButton(button);
|
||||
logger.info(
|
||||
"Registering",
|
||||
JSON.stringify(
|
||||
{ bdaddr: button.bdaddr, uniqId: constants.uniqId },
|
||||
null,
|
||||
4,
|
||||
),
|
||||
);
|
||||
ha.startLifeLine(
|
||||
"Button Controller Connected",
|
||||
constants.uniqId,
|
||||
haDevice,
|
||||
constants.LIFELINE_SIGNAL.objectId,
|
||||
);
|
||||
registerHAObjects(constants.actions, ha, constants.uniqId, haDevice, logger);
|
||||
registerHAObjects(constants.signals, ha, constants.uniqId, haDevice, logger);
|
||||
registerHAObjects(constants.states, ha, constants.uniqId, haDevice, logger);
|
||||
return constants;
|
||||
};
|
||||
|
||||
const registerHAObjects = (
|
||||
objs: Record<string, ButtonMqttEntity>,
|
||||
ha: HAmqtt,
|
||||
uniqId: string,
|
||||
haDevice: HADevice,
|
||||
logger: Logger,
|
||||
) => {
|
||||
Object.keys(objs).forEach((k) => {
|
||||
const o = objs[k];
|
||||
logger.info(
|
||||
"Registering",
|
||||
JSON.stringify({ entity: k, objcetId: o.objectId, uniqId }, null, 4),
|
||||
);
|
||||
ha.registerEntity(
|
||||
`Button ${o.objectId}`,
|
||||
o.component,
|
||||
uniqId,
|
||||
o.objectId,
|
||||
haDevice,
|
||||
o.additionalProps,
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
export const deregisterButton = (
|
||||
ha: HAmqtt,
|
||||
bdaddr: string,
|
||||
logger: Logger,
|
||||
) => {
|
||||
const constants = getConstants(ha, bdaddr);
|
||||
const uniqId = constants.uniqId;
|
||||
logger.info("Deregistering", JSON.stringify({ bdaddr, uniqId }, null, 4));
|
||||
deregisterHAObjects(constants.actions, ha, uniqId, logger);
|
||||
deregisterHAObjects(constants.signals, ha, uniqId, logger);
|
||||
deregisterHAObjects(constants.states, ha, uniqId, logger);
|
||||
};
|
||||
|
||||
const deregisterHAObjects = (
|
||||
objs: Record<string, ButtonMqttEntity>,
|
||||
ha: HAmqtt,
|
||||
uniqId: string,
|
||||
logger: Logger,
|
||||
) => {
|
||||
Object.keys(objs).forEach((k) => {
|
||||
const o = objs[k];
|
||||
logger.info(
|
||||
"Deregistering",
|
||||
JSON.stringify({ entity: k, objcetId: o.objectId, uniqId }, null, 4),
|
||||
);
|
||||
ha.deregisterEntity(o.component, uniqId, o.objectId);
|
||||
});
|
||||
};
|
||||
+12
-4
@@ -29,16 +29,23 @@ export function makeHAmqtt(
|
||||
) => {
|
||||
return `${options.topics.homeassistant}/${component}/${nodeId}/${objectId}`;
|
||||
};
|
||||
|
||||
const publishStateFromObject = (
|
||||
obj: FlicMqttPrefix,
|
||||
state: any,
|
||||
opt: MQTTPublishOpt = {},
|
||||
) => {
|
||||
mqttServer.publish(obj.mqttPrefix, state + "", opt);
|
||||
logger.debug(JSON.stringify({ obj, state, opt }));
|
||||
};
|
||||
const publishState = (
|
||||
nodeId: string,
|
||||
objectId: string,
|
||||
state: any,
|
||||
opt: MQTTPublishOpt = {},
|
||||
) => {
|
||||
const btntopic = genFlicPrefix(nodeId, objectId);
|
||||
mqttServer.publish(btntopic, state + "", opt);
|
||||
logger.debug(btntopic, state, JSON.stringify(opt));
|
||||
const topic = genFlicPrefix(nodeId, objectId);
|
||||
mqttServer.publish(topic, state + "", opt);
|
||||
logger.debug(topic, state, JSON.stringify(opt));
|
||||
};
|
||||
const registerEntity = (
|
||||
name: string,
|
||||
@@ -102,6 +109,7 @@ export function makeHAmqtt(
|
||||
deregisterEntity,
|
||||
registerEntity,
|
||||
publishState,
|
||||
publishStateFromObject,
|
||||
genFlicPrefix,
|
||||
genFlicPrefixObject,
|
||||
startLifeLine,
|
||||
|
||||
@@ -23,7 +23,7 @@ export const startFlicHubController = (
|
||||
{
|
||||
payload_available: "ON",
|
||||
payload_not_available: "unavailable",
|
||||
topic: constants.LIFELINE_SGINAL.mqttPrefix,
|
||||
topic: constants.LIFELINE_SIGNAL.mqttPrefix,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -33,7 +33,7 @@ export const startFlicHubController = (
|
||||
"FlicHub Connected",
|
||||
constants.NODE_ID,
|
||||
haDevice,
|
||||
constants.LIFELINE_SGINAL.objectId,
|
||||
constants.LIFELINE_SIGNAL.objectId,
|
||||
);
|
||||
ha.registerEntity(
|
||||
"Action Message",
|
||||
|
||||
@@ -23,7 +23,7 @@ export const getConstants = (ha: HAmqtt, options: FlicHubOptions) => {
|
||||
const nodeId = `${NODE_ID}${options.uniqueId}`;
|
||||
return {
|
||||
NODE_ID: nodeId,
|
||||
LIFELINE_SGINAL: ha.genFlicPrefixObject(nodeId, "lifeline"),
|
||||
LIFELINE_SIGNAL: ha.genFlicPrefixObject(nodeId, "lifeline"),
|
||||
MESSAGE: ha.genFlicPrefixObject(nodeId, "action-message"),
|
||||
COMMAND_TOPIC: (virtualId: string) =>
|
||||
ha.genFlicPrefixObject(nodeId, `virt-command-${virtualId}`),
|
||||
|
||||
+2
-3
@@ -1,11 +1,10 @@
|
||||
import { makeButtonController } from "./button_controller/index";
|
||||
|
||||
import { makeHAmqtt } from "./ha_mqtt/index";
|
||||
import { startIRController } from "./ir_controller/index";
|
||||
|
||||
import { makeLogger } from "./Logger";
|
||||
import { FlicMQTT } from "./mqtt_client/index";
|
||||
import { startFlicHubController } from "./hub_controller/index";
|
||||
import { startButtinStateHandler } from "./button_controller/index";
|
||||
|
||||
export const start = (options: Options) => {
|
||||
const mqttServer = new FlicMQTT(options.mqtt.host, {
|
||||
@@ -25,7 +24,7 @@ export const start = (options: Options) => {
|
||||
mqttServer.on("connected", () => {
|
||||
logger.info("connected to mqtt");
|
||||
if (!options.flicBtns?.disabled) {
|
||||
makeButtonController(ha, options.flicBtns).start();
|
||||
startButtinStateHandler(ha, options.flicBtns);
|
||||
}
|
||||
if (!options.flicIR?.disabled) {
|
||||
startIRController(ha, mqttServer, options.flicIR);
|
||||
|
||||
@@ -9,7 +9,7 @@ export const registerEntities = (
|
||||
) => {
|
||||
const {
|
||||
NODE_ID,
|
||||
LIFELINE_SGINAL,
|
||||
LIFELINE_SIGNAL,
|
||||
RECORD_SIGNAL,
|
||||
RECORD_SIGNAL_SET,
|
||||
PLAY_SIGNAL,
|
||||
@@ -22,7 +22,7 @@ export const registerEntities = (
|
||||
"IR Connnected",
|
||||
NODE_ID,
|
||||
haDevice,
|
||||
LIFELINE_SGINAL.objectId,
|
||||
LIFELINE_SIGNAL.objectId,
|
||||
);
|
||||
ha.registerEntity(
|
||||
"Record Signal",
|
||||
@@ -38,7 +38,7 @@ export const registerEntities = (
|
||||
},
|
||||
);
|
||||
ha.registerEntity(
|
||||
"Delete Signal",
|
||||
"Play Signal",
|
||||
"button",
|
||||
NODE_ID,
|
||||
PLAY_SIGNAL.objectId,
|
||||
|
||||
@@ -48,6 +48,6 @@ export const startIRController = (
|
||||
registerSelect(ha, haDevice, state, constants, onDone),
|
||||
);
|
||||
logger.debug("subscribing to", constants.set_topics);
|
||||
mqtt.subscribe(constants.set_topics.map((x) => x.mqttPrefix));
|
||||
mqtt.subscribe(constants.set_topics);
|
||||
logger.info("is up");
|
||||
};
|
||||
@@ -25,7 +25,7 @@ export type IRConstants = ReturnType<typeof getConstants>;
|
||||
export type IRConstantsNames = keyof IRConstants;
|
||||
export const getConstants = (ha: HAmqtt, options: IRControllerOpt) => {
|
||||
const nodeId = `${NODE_ID}${options.uniqueId}`;
|
||||
const LIFELINE_SGINAL = ha.genFlicPrefixObject(nodeId, "lifeline");
|
||||
const LIFELINE_SIGNAL = ha.genFlicPrefixObject(nodeId, "lifeline");
|
||||
const RECORD_SIGNAL = ha.genFlicPrefixObject(nodeId, "record");
|
||||
const RECORD_SIGNAL_SET = ha.genFlicPrefixObject(nodeId, "record/set");
|
||||
const PLAY_SIGNAL = ha.genFlicPrefixObject(nodeId, "play");
|
||||
@@ -38,12 +38,12 @@ export const getConstants = (ha: HAmqtt, options: IRControllerOpt) => {
|
||||
{
|
||||
payload_available: "ON",
|
||||
payload_not_available: "unavailable",
|
||||
topic: LIFELINE_SGINAL.mqttPrefix,
|
||||
topic: LIFELINE_SIGNAL.mqttPrefix,
|
||||
},
|
||||
];
|
||||
return {
|
||||
NODE_ID: nodeId,
|
||||
LIFELINE_SGINAL,
|
||||
LIFELINE_SIGNAL: LIFELINE_SIGNAL,
|
||||
RECORD_SIGNAL,
|
||||
RECORD_SIGNAL_SET,
|
||||
RECORDED_SIGNALS,
|
||||
@@ -57,7 +57,7 @@ export const getConstants = (ha: HAmqtt, options: IRControllerOpt) => {
|
||||
RECORDED_SIGNALS_CMD,
|
||||
PLAY_SIGNAL_SET,
|
||||
DELETE_SIGNAL_CMD,
|
||||
],
|
||||
].map((x) => x.mqttPrefix),
|
||||
availability,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user