export type ExportType = { 'homekit': HomekitExportType, 'openhab': string | string[] } export type HomekitExportType = { 'id': string, 'object': any } export function SwitchExport(itemId: string, label: string, stateTopic: string, stateFeedbackTopic: string, type: string) : ExportType { let homekitOut: HomekitExportType switch (type) { case 'bulb': homekitOut = SwitchHomekitBulbExport(itemId, label, stateTopic, stateFeedbackTopic) break case 'outlet': homekitOut = SwitchHomekitOutletExport(itemId, label, stateTopic, stateFeedbackTopic) break default: throw new Error(`no homekit export function for type ${type}`) } let openhabOut : string = SwitchOpenHABExport(itemId, label, stateTopic, stateFeedbackTopic) return { 'homekit': homekitOut, 'openhab': openhabOut } } export function ThermostatExport(itemId: string, label: string, temperatureTopic: string, temperatureFeedbackTopic: string, presetTemperatureTopic: string, presetTemperatureFeedbackTopic: string) : ExportType { return {'homekit': ThermostatHomekitExport(itemId, label, temperatureTopic, temperatureFeedbackTopic), 'openhab': ThermostatOpenHAPExport(itemId, label, temperatureTopic, temperatureFeedbackTopic, presetTemperatureTopic, presetTemperatureFeedbackTopic)} } export function ContactExport(itemId: string, label: string, status: string) : ExportType { return {'homekit': ContactHomekitExport(itemId, label, status), 'openhab': ContactOpenHABExport(itemId, label, status)} } 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': ''} } export function RelayBoxExport(itemId: string, stateTopicPre: string, feedbackTopicPre: string, conflictTopicPre: string, itemNames: string[]) : ExportType { return {'homekit': RelayBoxHomekitExport(itemId, stateTopicPre, feedbackTopicPre, conflictTopicPre, itemNames), 'openhab': RelayBoxOpenHABExport(itemId, stateTopicPre, feedbackTopicPre, conflictTopicPre, itemNames)} } export function SummerSwitchExport(summerSwitchTopic: string) : ExportType { return {'homekit': {'id':'', 'object': {}}, 'openhab': `Switch SummerSwitch "Winter?" {mqtt=">[localbroker:${summerSwitchTopic}:command:*:default],<[localbroker:${summerSwitchTopic}:state:default]"}`} } function RelayBoxHomekitExport(itemId: string, stateTopicPre: string, feedbackTopicPre: string, conflictTopicPre: string, itemNames: string[]) : HomekitExportType{ return {'id': '', 'object': {}} } function RelayBoxOpenHABExport(itemId: string, stateTopicPre: string, feedbackTopicPre: string, conflictTopicPre: string, itemNames: string[]) : string[] { let o : string[] = [] itemNames.forEach((label: string, index: number) => { o.push(`Switch ${itemId}${index} "${label}" {mqtt=">[localbroker:${stateTopicPre}/${index}:command:*:default],<[localbroker:${feedbackTopicPre}/${index}:state:default]"}`) o.push(`String ${itemId}${index}Conflict "${label} [%s]" {mqtt="<[localbroker:${conflictTopicPre}/${index}:state:default]"}`) }) return o } function SwitchHomekitBulbExport(id: string, label: string, setOn: string, statusOn: string) : HomekitExportType { let o : any = { "id": id, "name": label, "service": "Lightbulb", "topic": { "setOn": setOn, "statusOn": statusOn }, "payload": { "onTrue": "ON", "onFalse": "OFF", "brightnessFactor": "", "hueFactor": "", "saturationFactor": "" }, "config": {} } return { 'id': id, 'object': o } } function SwitchHomekitOutletExport(id: string, label: string, setOn: string, statusOn: string) : HomekitExportType { let o : any = { "id": id, "name": label, "service": "Outlet", "topic": { "setOn": setOn, "statusOn": statusOn }, "payload": { "onTrue": "ON", "onFalse": "OFF" }, "config": {} } return { 'id': id, 'object': o } } function ThermostatHomekitExport(id: string, label: string, setTemperature: string, statusTemperature: string) : HomekitExportType { let o : any = { "id": id, "name": label, "service": "Thermostat", "topic": { "setTargetTemperature": setTemperature, "statusTargetTemperature": statusTemperature, "statusCurrentTemperature": statusTemperature }, "payload": {} } return { 'id': id, 'object': o } } function ContactHomekitExport(id: string, label: string, status: string) : HomekitExportType { let o : any = { "id": id, "name": label, "service": "ContactSensor", "topic": { "statusContactSensorState": status }, "payload": { "onContactDetected": "CLOSED" } } return { 'id': id, 'object': o } } function SwitchOpenHABExport(id: string, label: string, setOn: string, statusOn: string): string { // Switch windowLightKitchen {mqtt=">[localbroker:nodered/items/windowLightKitchen:command:*:default]", mqtt="<[localbroker:nodered/items/windowLightKitchen/feedback:state:default]"} return `Switch ${id} "${label}"{mqtt=">[localbroker:${setOn}:command:*:default],<[localbroker:${statusOn}:state:default]"}` } function ContactOpenHABExport(id: string, label: string, status: string): string { // Switch windowLightKitchen {mqtt=">[localbroker:nodered/items/windowLightKitchen:command:*:default]", mqtt="<[localbroker:nodered/items/windowLightKitchen/feedback:state:default]"} return `Contact ${id} "${label}" {mqtt="<[localbroker:${status}:state:default]"}` } function ThermostatOpenHAPExport(id: string, label: string, setTemperature: string, statusTemperature: string, presetTemperature: string, presetStatusTemperature: string) : string[] { let o : string[] = [] o.push(`Number ${id} "${label} [%.1f °C]" {mqtt=">[localbroker:${setTemperature}:command:*:default],<[localbroker:${statusTemperature}:state:default]"}`) o.push(`Number Preset_${id} "Preset_${label} [%.1f °C]" {mqtt=">[localbroker:${presetTemperature}:command:*:default],<[localbroker:${presetStatusTemperature}:state:default]"}`) return o } 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} }