From 3b6353efa06c47a590555cf9f5a1e674464400c3 Mon Sep 17 00:00:00 2001 From: atrovato <1839717+atrovato@users.noreply.github.com> Date: Sun, 15 May 2022 01:07:26 +0200 Subject: [PATCH] Add channel trait --- .../lib/traits/googleActions.channel.trait.js | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 server/services/google-actions/lib/traits/googleActions.channel.trait.js diff --git a/server/services/google-actions/lib/traits/googleActions.channel.trait.js b/server/services/google-actions/lib/traits/googleActions.channel.trait.js new file mode 100644 index 0000000000..109e94a71f --- /dev/null +++ b/server/services/google-actions/lib/traits/googleActions.channel.trait.js @@ -0,0 +1,92 @@ +const { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../../../../utils/constants'); + +/** + * @see https://developers.google.com/assistant/smarthome/traits/channel + */ +const volumeTrait = { + key: 'action.devices.traits.Channel', + features: [ + { + category: DEVICE_FEATURE_CATEGORIES.TELEVISION, + type: DEVICE_FEATURE_TYPES.TELEVISION.CHANNEL, + }, + { + category: DEVICE_FEATURE_CATEGORIES.TELEVISION, + type: DEVICE_FEATURE_TYPES.TELEVISION.CHANNEL_UP, + }, + { + category: DEVICE_FEATURE_CATEGORIES.TELEVISION, + type: DEVICE_FEATURE_TYPES.TELEVISION.CHANNEL_DOWN, + }, + { + category: DEVICE_FEATURE_CATEGORIES.TELEVISION, + type: DEVICE_FEATURE_TYPES.TELEVISION.VOLUME_MUTE, + }, + ], + generateAttributes: (device) => { + const { has_feedback: hasFeedback = false } = device.features.find( + (feature) => + feature.category === DEVICE_FEATURE_CATEGORIES.TELEVISION && + feature.type === DEVICE_FEATURE_TYPES.TELEVISION.CHANNEL, + ); + + return { + commandOnlyVolume: !hasFeedback, + }; + }, + states: [], + commands: { + 'action.devices.commands.selectChannel': { + mute: { + writeValue: (paramValue) => { + return paramValue ? 1 : 0; + }, + getFeatures: () => { + return [ + { + category: DEVICE_FEATURE_CATEGORIES.TELEVISION, + type: DEVICE_FEATURE_TYPES.TELEVISION.VOLUME_MUTE, + }, + ]; + }, + }, + }, + 'action.devices.commands.setVolume': { + volumeLevel: { + writeValue: (paramValue) => { + return paramValue; + }, + getFeatures: () => { + return [ + { + category: DEVICE_FEATURE_CATEGORIES.TELEVISION, + type: DEVICE_FEATURE_TYPES.TELEVISION.VOLUME, + }, + ]; + }, + }, + }, + 'action.devices.commands.volumeRelative': { + relativeSteps: { + writeValue: () => { + return 1; + }, + getFeatures: (paramValue) => { + const feature = { + category: DEVICE_FEATURE_CATEGORIES.TELEVISION, + type: DEVICE_FEATURE_TYPES.TELEVISION.VOLUME_UP, + }; + if (paramValue < 0) { + feature.type = DEVICE_FEATURE_TYPES.TELEVISION.VOLUME_DOWN; + } + + return Array(Math.abs(paramValue)).fill(feature); + }, + }, + }, + }, +}; + +module.exports = { + volumeTrait, +};