UNPKG

matterbridge-shelly

Version:
65 lines (64 loc) 4.53 kB
import { db, debugStringify, hk, idn, nf, or, rs, YELLOW } from 'matterbridge/logger'; import { isValidNumber, isValidObject } from 'matterbridge/utils'; export function shellyIdentifyCommandHandler(endpoint, component, request) { endpoint.log.info(`Identify command received for endpoint ${or}${endpoint.name}${nf}:${or}${endpoint.number}${nf} component ${hk}${component.name}${nf}:${hk}${component.id}${nf} request ${debugStringify(request)}`); } export function shellySwitchCommandHandler(endpoint, switchComponent, command) { if (command === 'On') switchComponent.On(); else if (command === 'Off') switchComponent.Off(); else if (command === 'Toggle') switchComponent.Toggle(); endpoint.log.info(`${db}Sent command ${hk}${switchComponent.name}${db}:${hk}${switchComponent.id}${db}:${hk}${command}()${db} to shelly device ${idn}${switchComponent.device.id}${rs}${db}`); } export function shellyLightCommandHandler(endpoint, lightComponent, command, level, color, colorTemp) { if (command === 'On') lightComponent.On(); else if (command === 'Off') lightComponent.Off(); else if (command === 'Toggle') lightComponent.Toggle(); if (command === 'On' || command === 'Off' || command === 'Toggle') endpoint.log.info(`${db}Sent command ${hk}${lightComponent.name}${db}:${hk}${lightComponent.id}${db}:${hk}${command}()${db} to shelly device ${idn}${lightComponent.device.id}${rs}${db}`); if (command === 'Level' && isValidNumber(level, 1, 254)) { const shellyLevel = Math.max(Math.min(Math.round((level / 254) * 100), 100), 1); lightComponent.Level(shellyLevel); endpoint.log.info(`${db}Sent command ${hk}${lightComponent.name}${db}:${hk}${lightComponent.id}${db}:${hk}Level(${YELLOW}${shellyLevel}${hk})${db} to shelly device ${idn}${lightComponent.device.id}${rs}${db}`); } if (command === 'ColorRGB' && isValidObject(color, 3, 3)) { color.r = Math.max(Math.min(color.r, 255), 0); color.g = Math.max(Math.min(color.g, 255), 0); color.b = Math.max(Math.min(color.b, 255), 0); lightComponent.ColorRGB(color.r, color.g, color.b); endpoint.log.info(`${db}Sent command ${hk}${lightComponent.name}${db}:${hk}${lightComponent.id}${db}:${hk}ColorRGB(${YELLOW}${color.r}${hk}, ${YELLOW}${color.g}${hk}, ${YELLOW}${color.b}${hk})${db} to shelly device ${idn}${lightComponent.device.id}${rs}${db}`); } if (command === 'ColorTemp' && isValidNumber(colorTemp, 147, 500)) { const minColorTemp = 147; const maxColorTemp = 500; const minTemp = lightComponent.device.model === 'SHBDUO-1' ? 2700 : 3000; const maxTemp = 6500; const temp = Math.max(Math.min(Math.round(((colorTemp - minColorTemp) / (maxColorTemp - minColorTemp)) * (minTemp - maxTemp) + maxTemp), maxTemp), minTemp); lightComponent.ColorTemp(temp); endpoint.log.info(`${db}Sent command ${hk}${lightComponent.name}${db}:${hk}${lightComponent.id}${db}:${hk}ColorTemp(for model ${lightComponent.device.model} ${YELLOW}${colorTemp}${hk}->${YELLOW}${temp}${hk})${db} to shelly device ${idn}${lightComponent.device.id}${rs}${db}`); } } export function shellyCoverCommandHandler(endpoint, coverComponent, command, pos) { if (command === 'Stop') { endpoint.log.info(`${db}Sent command ${hk}${coverComponent.name}${db}:${hk}${coverComponent.id}${db}:${hk}${command}()${db} to shelly device ${idn}${coverComponent.device.id}${rs}${db}`); coverComponent.Stop(); } else if (command === 'Open') { endpoint.log.info(`${db}Sent command ${hk}${coverComponent.name}${db}:${hk}${coverComponent.id}${db}:${hk}${command}()${db} to shelly device ${idn}${coverComponent.device.id}${rs}${db}`); coverComponent.Open(); } else if (command === 'Close') { endpoint.log.info(`${db}Sent command ${hk}${coverComponent.name}${db}:${hk}${coverComponent.id}${db}:${hk}${command}()${db} to shelly device ${idn}${coverComponent.device.id}${rs}${db}`); coverComponent.Close(); } else if (command === 'GoToPosition' && isValidNumber(pos, 0, 10000)) { const shellyPos = 100 - Math.max(Math.min(Math.round(pos / 100), 100), 0); endpoint.log.info(`${db}Sent command ${hk}${coverComponent.name}${db}:${hk}${coverComponent.id}${db}:${hk}${command}(${YELLOW}${shellyPos}${hk})${db} to shelly device ${idn}${coverComponent.device.id}${rs}${db}`); coverComponent.GoToPosition(shellyPos); } }