Skip to content

Commit

Permalink
feat(discovery): add supported_color_modes to lights
Browse files Browse the repository at this point in the history
Fixes #3894
  • Loading branch information
robertsLando committed Sep 16, 2024
1 parent cd206ce commit d38d2ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
12 changes: 12 additions & 0 deletions api/hass/configurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ type HassDeviceKey =
| 'config_switch'
| 'config_number'

export type ColorMode =
| 'unknown'
| 'onoff'
| 'brightness'
| 'color_temp'
| 'hs'
| 'xy'
| 'rgb'
| 'rgbw'
| 'rgbww'
| 'white'

const configurations: Record<HassDeviceKey, HassDevice> = {
// Binary sensor https://www.home-assistant.io/components/binary_sensor.mqtt
binary_sensor: {
Expand Down
24 changes: 19 additions & 5 deletions api/lib/Gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AlarmSensorType, SetValueAPIOptions } from 'zwave-js'
import { CommandClasses, ValueID } from '@zwave-js/core'
import * as Constants from './Constants'
import { LogLevel, module } from './logger'
import hassCfg from '../hass/configurations'
import hassCfg, { ColorMode } from '../hass/configurations'
import hassDevices from '../hass/devices'
import { storeDir } from '../config/app'
import { IClientPublishOptions } from 'mqtt'
Expand Down Expand Up @@ -1286,6 +1286,9 @@ export default class Gateway {
cfg.discovery_payload.payload_close = 0
} else {
cfg = utils.copy(hassCfg.light_dimmer)
cfg.discovery_payload.supported_color_modes = [
'brightness',
] as ColorMode[]
cfg.discovery_payload.brightness_state_topic =
getTopic
cfg.discovery_payload.brightness_command_topic =
Expand Down Expand Up @@ -2623,6 +2626,13 @@ export default class Gateway {

const endpoint = currentColorValue.endpoint

// https://github.com/home-assistant/core/blob/2e76b1f834ea26ef3e1726930812cb4c2ea82518/homeassistant/components/light/__init__.py#L65C1-L81C48
const supportedColors: ColorMode[] = []

cfg.discovery_payload.supported_color_modes = supportedColors

supportedColors.push('rgb')

// current color values are automatically added later in discoverValue function
cfg.values = []

Expand All @@ -2646,10 +2656,11 @@ export default class Gateway {
switchValue = `37-${endpoint}-currentValue`
}

/* Find the control switch of the device Brightness or Binary
If multilevel is not there use binary
Some devices use also endpoint + 1 as on/off/brightness... try to guess that too!
*/
/*
Find the control switch of the device Brightness or Binary
If multilevel is not there use binary
Some devices use also endpoint + 1 as on/off/brightness... try to guess that too!
*/
let discoveredStateTopic: string
let discoveredCommandTopic: string

Expand Down Expand Up @@ -2677,12 +2688,14 @@ export default class Gateway {
}

if (brightnessValue) {
supportedColors.push('brightness')
cfg.discovery_payload.brightness_state_topic = discoveredStateTopic
cfg.discovery_payload.brightness_command_topic =
discoveredCommandTopic
cfg.discovery_payload.state_topic = discoveredStateTopic
cfg.discovery_payload.command_topic = discoveredCommandTopic
} else if (switchValue) {
supportedColors.push('onoff')
cfg.discovery_payload.state_topic = discoveredStateTopic
cfg.discovery_payload.command_topic = discoveredCommandTopic

Expand All @@ -2695,6 +2708,7 @@ export default class Gateway {

// if whitevalue exists, use currentColor value to get/set white
if (whiteValue && currentColorValue) {
supportedColors.push('white')
// still use currentColor but change the template
cfg.discovery_payload.color_temp_state_topic =
cfg.discovery_payload.rgb_state_topic
Expand Down

0 comments on commit d38d2ff

Please sign in to comment.