From b979969324a331fe9536a7ebe50a62bd97778af3 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Sat, 27 Jan 2018 17:40:33 +0100 Subject: [PATCH] hue light --- src/Export.ts | 45 ++++++++++++++++++ ...eratureBulbItem.ts => HueColorBulbItem.ts} | 46 +++++++++++++++++-- src/main.ts | 9 +++- 3 files changed, 94 insertions(+), 6 deletions(-) rename src/{HueColorTemperatureBulbItem.ts => HueColorBulbItem.ts} (56%) diff --git a/src/Export.ts b/src/Export.ts index 2465f37..abb4ebb 100644 --- a/src/Export.ts +++ b/src/Export.ts @@ -35,6 +35,16 @@ export function ContactExport(itemId: string, label: string, status: string) : E return {'homekit': ContactHomekitExport(itemId, label, status), 'openhab': ''} } +export function HueColorLightExport(itemId: string, label: string, + stateTopic: string, stateFeedbackTopic: string, + brightnessTopic: string, brightnessFeedbackTopic: string, + hueTopic: string, hueFeedbackTopic: string, + saturationTopic: string, saturationFeedbackTopic: string, + colorTemperatureTopic: string, colorTemperatureFeedbackTopic: string) { + return {'homekit': HueColorLightHomekitExport(itemId, label, stateTopic, stateFeedbackTopic, + brightnessTopic, brightnessFeedbackTopic, hueTopic, hueFeedbackTopic, + saturationTopic, saturationFeedbackTopic, colorTemperatureTopic, colorTemperatureFeedbackTopic), 'openhab': ''} +} function SwitchHomekitBulbExport(id: string, label: string, setOn: string, statusOn: string) : HomekitExportType { let o : any = { "id": id, @@ -109,3 +119,38 @@ function SwitchOpenHABExport(id: string, label: string, setOn: string, statusOn: // Switch windowLightKitchen {mqtt=">[localbroker:nodered/items/windowLightKitchen:command:*:default]", mqtt="<[localbroker:nodered/items/windowLightKitchen/feedback:state:default]"} return `Switch ${id} {mqtt=">[localbroker:${setOn}:command:*:default],<[localbroker:${statusOn}:state:default]"}` } + +function HueColorLightHomekitExport(id: string, label: string, + stateTopic: string, stateFeedbackTopic: string, + brightnessTopic: string, brightnessFeedbackTopic: string, + hueTopic: string, hueFeedbackTopic: string, + saturationTopic: string, saturationFeedbackTopic: string, + colorTemperatureTopic: string, colorTemperatureFeedbackTopic: string) { + let o: any = { + "id": id, + "name": label, + "service": "Lightbulb", + "manufacturer": "hue2mqtt - Hue", + "model": "color light", + "topic": { + "setOn": stateTopic, + "statusOn": stateFeedbackTopic, + "setBrightness": brightnessTopic, + "statusBrightness": brightnessFeedbackTopic, + "setHue": hueTopic, + "statusHue": hueFeedbackTopic, + "setSaturation": saturationTopic, + "statusSaturation": saturationFeedbackTopic, + "setColorTemperature": colorTemperatureTopic, + "statusColorTemperature": colorTemperatureFeedbackTopic + }, + "payload": { + "onTrue": "ON", + "onFalse": "OFF", + "brightnessFactor": 1, + "hueFactor": 1, + "saturationFactor": 1 + } + } + return { 'id': id, 'object': o} +} \ No newline at end of file diff --git a/src/HueColorTemperatureBulbItem.ts b/src/HueColorBulbItem.ts similarity index 56% rename from src/HueColorTemperatureBulbItem.ts rename to src/HueColorBulbItem.ts index 26b2edf..53a31da 100644 --- a/src/HueColorTemperatureBulbItem.ts +++ b/src/HueColorBulbItem.ts @@ -1,19 +1,28 @@ import * as logger from './log' import { mqttHandler } from './MqttDispatcher' import { AHomegearItem } from './AHomegearItem' +import { HueColorLightExport, ExportType } from './Export' -export class HueColorTemperatureBulbItem extends AHomegearItem { +export class HueColorBulbItem extends AHomegearItem { private bright: number private colorTemperature: number + private hue: number + private saturation: number private state: string private stateActionTopic: string private brightActionTopic: string private colorTemperatureActionTopic: string + private hueActionTopic: string + private saturationActionTopic: string private brightFeedbackTopic: string private stateFeedbackTopic: string private colorTemperatureFeedbackTopic: string + private hueFeedbackTopic: string + private saturationFeedbackTopic: string private brightTopic: string private stateTopic: string + private hueTopic: string + private saturationTopic: string private colorTemperatureTopic: string constructor(floor: string, room: string, item: string, label: string, hmId: number) { @@ -21,20 +30,39 @@ export class HueColorTemperatureBulbItem extends AHomegearItem { this.stateTopic = `${this.topicFirstPart}/state` this.brightTopic = `${this.topicFirstPart}/bright` this.colorTemperatureTopic = `${this.topicFirstPart}/colorTemperature` + this.hueTopic = `${this.topicFirstPart}/hue` + this.saturationTopic = `${this.topicFirstPart}/saturation` this.stateFeedbackTopic = `${this.topicFirstPart}/state/feedback` this.brightFeedbackTopic = `${this.topicFirstPart}/bright/feedback` + this.hueFeedbackTopic = `${this.topicFirstPart}/bright/hue` + this.saturationFeedbackTopic = `${this.topicFirstPart}/bright/saturation` this.colorTemperatureFeedbackTopic = `${this.topicFirstPart}/colorTemperature/feedback` this.stateActionTopic = `${this.actionTopicPre}/1/STATE` this.brightActionTopic = `${this.actionTopicPre}/1/BRIGHTNESS` + this.hueActionTopic = `${this.actionTopicPre}/1/HUE` + this.saturationActionTopic = `${this.actionTopicPre}/1/SATURATION` this.colorTemperatureActionTopic = `${this.actionTopicPre}/1/COLOR_TEMPERATURE` this.subscribeTopics = [ this.stateTopic, this.brightTopic, - this.colorTemperatureTopic + this.colorTemperatureTopic, + this.hueTopic, + this.saturationTopic ] this.state = 'OFF' this.bright = 0 this.colorTemperature = 0 + this.hue = 0 + this.saturation = 0 + } + + exportItem() : ExportType|null { + return HueColorLightExport(this.itemId, this.label, + this.stateTopic, this.stateFeedbackTopic, + this.brightTopic, this.brightFeedbackTopic, + this.hueTopic, this.hueFeedbackTopic, + this.saturationTopic, this.saturationFeedbackTopic, + this.colorTemperatureTopic, this.colorTemperatureFeedbackTopic) } @@ -50,9 +78,19 @@ export class HueColorTemperatureBulbItem extends AHomegearItem { } break case this.brightTopic: - this.bright = parseInt(payload) + this.bright = parseFloat(payload) mqttHandler.send(this.brightFeedbackTopic, `${this.bright}`) - mqttHandler.send(this.brightActionTopic, `${this.bright}`) + mqttHandler.send(this.brightActionTopic, `${this.bright} * 2.54`) + break + case this.hueTopic: + this.hue = parseFloat(payload) + mqttHandler.send(this.hueFeedbackTopic, `${this.hue}`) + mqttHandler.send(this.hueActionTopic, `${this.hue} * 65535.0 / 360.0`) + break + case this.saturationTopic: + this.saturation = parseFloat(payload) + mqttHandler.send(this.saturationFeedbackTopic, `${this.saturation}`) + mqttHandler.send(this.saturationActionTopic, `${this.saturation} * 2.54`) break case this.colorTemperatureTopic: this.colorTemperature = parseInt(payload) diff --git a/src/main.ts b/src/main.ts index 1a70125..f741248 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,7 +19,7 @@ import { MaxThermostat } from './MaxThermostat' import { MaxWindowContact } from './MaxWindowContact' import { UrlSwitchItem } from './UrlSwitchItem' import { Cron } from './Cron' -import { HueColorTemperatureBulbItem } from './HueColorTemperatureBulbItem' +import { HueColorBulbItem } from './HueColorBulbItem' logger.info("Dispatcher starting") @@ -84,8 +84,9 @@ let diningRoomShelfLight = new UrlSwitchItem('Gnd', 'DiningRoom', 'ShelfLight', diningRoomShelfLight.start() allLabeledItems.push(diningRoomShelfLight) -let diningRoomNaehkaestchenLight = new HueColorTemperatureBulbItem('Gnd', 'DiningRoom', 'NaehkaestchenLight', 'Lampe Naehkaestchen', 15) +let diningRoomNaehkaestchenLight = new HueColorBulbItem('Gnd', 'DiningRoom', 'NaehkaestchenLight', 'Lampe Naehkaestchen', 15) diningRoomNaehkaestchenLight.start() +allLabeledItems.push(diningRoomNaehkaestchenLight) // Wohnzimmer ----------------------------------------------------------------------------------------------- // Wohnzimmer grosse Lampe 65557 24 1 65556 24 1 @@ -144,6 +145,10 @@ let bedRoomWolfgangsSide = new M433SwitchItem('1st', 'BedRoom', 'WolfgangsSide', bedRoomWolfgangsSide.start() allLabeledItems.push(bedRoomWolfgangsSide) +let bedRoomWolfgangBedLight = new HueColorBulbItem('1st', 'BedRoom', 'WolfgangBedLight', 'Bettlicht', 15) +bedRoomWolfgangBedLight.start() +allLabeledItems.push(bedRoomWolfgangBedLight) + // Schlafzimmer Pattys Seite 13980756 24 1 13980753 24 1 let bedRoomPattysSide = new M433SwitchItem('1st', 'BedRoom', 'PattysSide', 'Pattys Seite Schlafzimmer', '13980756 24 1', '13980753 24 1') bedRoomPattysSide.start()