diff --git a/README.md b/README.md index 9551b9a..a4b279f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Flic2Hass +# Flic2Hass ![GitHub package.json version (subfolder of monorepo)](https://img.shields.io/github/package-json/v/asosnovsky/flic2hass) This is a complete refactor of various previous versions of flic2hass. This version provides more features and even supports the onboard IR module! @@ -10,6 +10,7 @@ This is a complete refactor of various previous versions of flic2hass. This vers * Show connectivity state * Show click action types: `click` `hold` `double_click` +* Device emits automation events * Detect presses: `released` `pressed` * Battery Logging * Extract firmware & sofware version numbers diff --git a/lib.js b/lib.js index ffdca48..8c4aa0b 100644 --- a/lib.js +++ b/lib.js @@ -136,6 +136,30 @@ var ENTITIES = { expire_after: 5, name: "Passive Mode" } + ], + button_short_press: [ + "device_automation", + { + type: "button_short_press", + subtype: "button_1", + automation_type: "trigger" + } + ], + button_long_press: [ + "device_automation", + { + type: "button_long_press", + subtype: "button_1", + automation_type: "trigger" + } + ], + button_double_press: [ + "device_automation", + { + type: "button_double_press", + subtype: "button_1", + automation_type: "trigger" + } ] }; function makeButtonController(ha, buttonModule) { @@ -186,6 +210,9 @@ function makeButtonController(ha, buttonModule) { avl.availability[1] ]; } + if (ENTITIES[objectId][0] === "device_automation") { + avl = {}; + } ha.registerEntity("Button ".concat(objectId), ENTITIES[objectId][0], genButtonUniqueId(button.bdaddr), objectId, haDevice, _object_spread$3({}, ENTITIES[objectId][1], avl)); }); }; @@ -200,6 +227,13 @@ function makeButtonController(ha, buttonModule) { }; var publishButtonAction = function(button, state) { ha.publishState(genButtonUniqueId(button.bdaddr), "action", state); + if (state === "click") { + ha.publishState(genButtonUniqueId(button.bdaddr), "button_short_press", "ON"); + } else if (state === "double_click") { + ha.publishState(genButtonUniqueId(button.bdaddr), "button_double_press", "ON"); + } else if (state === "hold") { + ha.publishState(genButtonUniqueId(button.bdaddr), "button_long_press", "ON"); + } }; var publishButtonMeta = function(button) { ha.publishState(genButtonUniqueId(button.bdaddr), "battery", button.batteryStatus); @@ -358,10 +392,14 @@ function makeHAmqtt(mqttServer) { var registerEntity = function(name, component, nodeId, objectId, device) { var additionalProps = arguments.length > 5 && arguments[5] !== void 0 ? arguments[5] : {}; var configtopic = genHAPrefix(component, nodeId, objectId) + "/config"; + if (component === "device_automation") { + additionalProps.topic = genFlicPrefix(nodeId, objectId); + } else { + additionalProps.state_topic = genFlicPrefix(nodeId, objectId); + } var configObj = _object_spread_props$1(_object_spread$2({ name: name }, additionalProps), { - state_topic: genFlicPrefix(nodeId, objectId), unique_id: "Flic_".concat(nodeId, "_").concat(objectId), device: device }); diff --git a/src/ButtonController.ts b/src/ButtonController.ts index de5c526..4153dbc 100644 --- a/src/ButtonController.ts +++ b/src/ButtonController.ts @@ -22,6 +22,18 @@ const ENTITIES: Record]> = { "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' } + ] } export type ButtonController = ReturnType; export function makeButtonController( @@ -41,6 +53,7 @@ export function makeButtonController( model: `v${button.flicVersion}_${button.color.trim().length > 0 ? button.color : 'white'}`, sw: String(button.firmwareVersion), hw: String(button.flicVersion), + configuration_url: "https://hubsdk.flic.io/", } } const genButtonUniqueId = (bdaddr: string): string => bdaddr.replace(/:/g, '_') @@ -48,7 +61,7 @@ export function makeButtonController( logger.info('Registering', JSON.stringify(button, null, 4)) const haDevice = getDeviceFromButton(button); Object.keys(ENTITIES).forEach(objectId => { - let avl = { + let avl: any = { availability: [ { payload_available: 'ON', @@ -69,6 +82,9 @@ export function makeButtonController( if (objectId === 'ready' || objectId == 'connected') { avl.availability = [avl.availability[1]] } + if (ENTITIES[objectId][0] === 'device_automation') { + avl = {} + } ha.registerEntity( `Button ${objectId}`, ENTITIES[objectId][0], @@ -95,8 +111,15 @@ export function makeButtonController( const publishButtonState = (button: Button, state: 'released' | 'pressed') => { ha.publishState(genButtonUniqueId(button.bdaddr), 'state', state); } - const publishButtonAction = (button: Button, state: string) => { + const publishButtonAction = (button: Button, state: 'click' | 'double_click' | 'hold') => { ha.publishState(genButtonUniqueId(button.bdaddr), 'action', state); + if (state === 'click') { + ha.publishState(genButtonUniqueId(button.bdaddr), 'button_short_press', 'ON') + } else if (state === 'double_click') { + ha.publishState(genButtonUniqueId(button.bdaddr), 'button_double_press', 'ON') + } else if (state === 'hold') { + ha.publishState(genButtonUniqueId(button.bdaddr), 'button_long_press', 'ON') + } } const publishButtonMeta = (button: Button) => { ha.publishState(genButtonUniqueId(button.bdaddr), 'battery', button.batteryStatus); diff --git a/src/HAmqtt.ts b/src/HAmqtt.ts index dc74613..c9edefe 100644 --- a/src/HAmqtt.ts +++ b/src/HAmqtt.ts @@ -19,8 +19,9 @@ export type HADevice = { model: string, sw?: string, hw?: string, + configuration_url?: string, } -export type HAComponent = 'sensor' | 'binary_sensor' | 'button' | 'switch' | 'text'; +export type HAComponent = 'sensor' | 'binary_sensor' | 'button' | 'switch' | 'text' | 'device_automation'; export const makeOptions = (opt: Partial): HAmqttOptions => ({ debug: false, ...opt, @@ -61,10 +62,14 @@ export function makeHAmqtt( additionalProps: Record = {}, ) => { const configtopic = genHAPrefix(component, nodeId, objectId) + "/config" + if (component === 'device_automation') { + additionalProps.topic = genFlicPrefix(nodeId, objectId) + } else { + additionalProps.state_topic = genFlicPrefix(nodeId, objectId) + } const configObj = { name, ...additionalProps, - state_topic: genFlicPrefix(nodeId, objectId), unique_id: `Flic_${nodeId}_${objectId}`, device, }; diff --git a/src/IRController.ts b/src/IRController.ts index afcf763..99fc5d6 100644 --- a/src/IRController.ts +++ b/src/IRController.ts @@ -36,7 +36,8 @@ export const makeIRController = ( name: 'IR', manufacturer: 'Flic', model: `${NODE_ID}${options.uniqueId}`, - identifiers: ['FlicHubIR'] + identifiers: ['FlicHubIR'], + configuration_url: "https://hubsdk.flic.io/", } const nodeId = `${NODE_ID}${options.uniqueId}`; const LIFELINE_SGINAL = ha.genFlicPrefix(nodeId, 'lifeline');