26 KiB
ha-bridge
Emulates Philips Hue api to other home automation gateways such as an Amazon Echo. The Bridge has helpers to build devices for the gateway for the Logitech Harmony Hub, Vera, Vera Lite or Vera Edge. Alternatively the Bridge supports custom calls as well. The Bridge handles basic commands such as "On", "Off" and "brightness" commands of the hue protocol.
Build
To customize and build it yourself, build a new jar with maven:
mvn install
Otherwise, downloads are available at https://github.com/bwssytems/ha-bridge/releases.
Run
Then locate the jar and start the server with:
java -jar -Dvera.address=X.Y.Z.A -Dharmony.address=X.Y.Z.A -Dharmony.user=myself -Dharmony.pwd=passwd ha-bridge-0.X.Y.jar
Available Arguments
-Dvera.address=<ip address>
The argument for the vera address should be given as it the system does not have a way to find the address. Supply -Dvera.address=X.Y.Z.A on the command line to provide it. If a vera is not used, do not set it.
-Dupnp.config.address=<ip address>
The server defaults to the first available address on the host. Replace the -Dupnp.config.address=<ip address> value with the server ipv4 address you would like to use as the address that any upnp device will call after discovery.
-Dserver.port=<port>
The server defaults to running on port 8080. If you're already running a server (like openHAB) on 8080, -Dserver.port=<port> on the command line.
-Dupnp.device.db=<filepath>
The default location for the db to contain the devices as they are added is "data/devices.db". If you would like a different filename or directory, specify -Dupnp.devices.db=<directory>/<filename> or <filename> if it is the same directory.
-Dupnp.response.port=<port>
The upnp response port that will be used. The default is 50000.
-Dharmony.address=<ip address>
The argument for the Harmony Hub address should be given as the system does not have a way to find the address. Supply -Dharmony.address=X.Y.Z.A on the command line to provide it. If a Harmony Hub is not used, do not set it.
-Dharmony.user=<username>
The user name of the MyHarmony.com account for the Harmony Hub. This needs to be given if you are using the Harmony Hub Features, provide -Dharmony.user=<username> on the command line.
-Dharmony.pwd=<password>
The password for the user name of the MyHarmony.com account for the Harmony Hub. This needs to be given if you are using the Harmony Hub Features, provide -Dharmony.pwd=<password> on the command line.
-Dupnp.strict=<true|false>
Upnp has been very closed on this platform to try and respond as a hue and there is now a setting to control if it is more open or strict, Add -Dupnp.strict=<true|false> to your command line to have the emulator respond to what it thinks is an echo to a hue or any other device. The default is upnp.strict=true.
-Dtrace.upnp=<true|false>
Turn on tracing for upnp discovery messages. The default is false.
Web Config
Configure by going to the url for the host you are running on or localhost with port you have assigned:
http://<ip address>:<port>
Configuration REST API usage
This section will describe the REST api available for configuration. The REST body examples are all formatted for easy reading, the actual body usage should be like this:
{"var1":"value1","var2":"value2","var3:"value3"}
The body should be all in one string and not separated by returns, tabs or spaces. FYI, GET items do not require a body element. If you would like to see example return of json data for full Harmony Hub configuration if configured, which includes activities and devices, take a look at the resource file config.data. If you are interested in how the json data looks for the HA bridge configuration, after creating a device, look at the data directory for the device.db. These calls can be accomplished with a REST tool using the following URLs and HTTP Verb types:
Add a device
Add a new device to the HA Bridge configuration. There is a basic examples and then three alternate examples for the add. Please note that dimming is supported as well as custom value based on the dimming number given from the echo. This is under the Dimming and Value example.
POST http://host:8080/api/devices
Body arguments
| Name | Type | Description | Required |
|---|---|---|---|
| name | string | A name for the device. This is also the utterance value that the Echo will use. | Required |
| deviceType | string | This identifies what type of device entry this is. It is used by the system and should be the values of "switch", "scene", "custom", "activity" or "button". | Required |
| onUrl | string | This is the URL that is executed for an "on" request and is used for the device types of "switch", "scene" and "custom". For "activity" or "button" it is the Json target generated by the Harmony Hub helpers. | Required |
| offUrl | string | This is the URL that is executed for an "off" request and is used for the device types of "switch", "scene" and "custom". For "activity" or "button" it is the Json target generated by the Harmony Hub helpers. | Required |
| httpVerb | string | This is used for "custom" calls that the user would like to execute. The values can only be "GET, "PUT", "POST". | Optional |
| contentType | string | This is an http type string such as "application/text" or "application/xml" or "application/json". | Optional |
| contentBody | string | This is the content body that you would like to send when executing an "on" request. | Optional |
| contentBodyOff | string | This is the content body that you would like to send when executing an "off" request. | Optional |
Basic example
{
"name" : "bedroom light",
"deviceType" : "switch",
"onUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=41",
"offUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=41"
}
Dimming and value passing control example
Dimming is also supported by using the expressions ${intensity.percent} for 0-100 or ${intensity.byte} for 0-255 or custom values using ${intensity.math(<your expression using "X" as the value to operate on>)} i.e. "${intensity.math(X/4)}".
e.g.
{
"name": "entry light",
"deviceType": "switch",
"offUrl": "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=31",
"onUrl": "http://192.168.1.201:3480/data_request?id=action&output_format=json&DeviceNum=31&serviceId=urn:upnp-org:serviceId:Dimming1&action=SetLoadLevelTarget&newLoadlevelTarget=${intensity.percent}"
}
See the echo's documentation for the dimming phrase.
POST/PUT support example
This will allow control of any other application that may need more then GET. You can also use the dimming and value control commands within the URLs as well.
e.g:
{
"name": "test device",
"deviceType": "switch",
"offUrl": "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=31",
"onUrl": "http://192.168.1.201:3480/data_request?id=action&output_format=json&DeviceNum=31&serviceId=urn:upnp-org:serviceId:Dimming1&action=SetLoadLevelTarget&newLoadlevelTarget=${intensity.percent}",
"httpVerb":"POST",
"contentType" : "application/json",
"contentBody" : "{\"fooBar\":\"baz_on\"}"
"contentBodyOff" : "{\"fooBar\":\"baz_off\"}"
}
Custom Usage URLs Example
Anything that takes an action as a result of an HTTP request will probably work and you can also use the dimming and value control commands within the URLs as well - like putting Vera in and out of night mode:
{
"name": "night mode",
"deviceType": "switch",
"offUrl": "http://192.168.1.201:3480/data_request?id=lu_action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=SetHouseMode&Mode=1",
"onUrl": "http://192.168.1.201:3480/data_request?id=lu_action&serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1&action=SetHouseMode&Mode=3"
}
Response
| Name | Type | Description |
|---|---|---|
| id | number | This is the ID assigned to the device and used for lookup. |
| name | string | A name for the device. This is also the utterance value that the Echo will use. |
| deviceType | string | This identifies what type of device entry this is. It is used by the system and should be the values of "switch", "scene", "custom", "activity" or "button". |
| onUrl | string | This is the URL that is executed for an "on" request and is used for the device types of "switch", "scene" and "custom". For "activity" or "button" it is the Json target generated by the Harmony Hub helpers. |
| offUrl | string | This is the URL that is executed for an "off" request and is used for the device types of "switch", "scene" and "custom". For "activity" or "button" it is the Json target generated by the Harmony Hub helpers. |
| httpVerb | string | This is used for "custom" calls that the user would like to execute. The values can only be "GET, "PUT", "POST". |
| contentType | string | This is an http type string such as "application/text" or "application/xml" or "application/json". |
| contentBody | string | This is the content body that you would like to send when executing an "on" request. |
| contentBodyOff | string | This is the content body that you would like to send when executing an "off" request. |
{
"id" : 12345,
"name" : "bedroom light",
"deviceType" : "switch",
"onUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=41",
"offUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=41"
}
Update a device
Update an existing device using it's ID that was given when the device was created and the update could contain any of the fields that are used and shown in the previous examples when adding a device.
POST http://host:8080/api/devices/<id>
Body arguments
| Name | Type | Description | Required |
|---|---|---|---|
| id | number | This is the ID assigned to the device and used for lookup. | |
| name | string | A name for the device. This is also the utterance value that the Echo will use. | Required |
| deviceType | string | This identifies what type of device entry this is. It is used by the system and should be the values of "switch", "scene", "custom", "activity" or "button". | Required |
| onUrl | string | This is the URL that is executed for an "on" request and is used for the device types of "switch", "scene" and "custom". For "activity" or "button" it is the Json target generated by the Harmony Hub helpers. | Required |
| offUrl | string | This is the URL that is executed for an "off" request and is used for the device types of "switch", "scene" and "custom". For "activity" or "button" it is the Json target generated by the Harmony Hub helpers. | Required |
| httpVerb | string | This is used for "custom" calls that the user would like to execute. The values can only be "GET, "PUT", "POST". | Optional |
| contentType | string | This is an http type string such as "application/text" or "application/xml" or "application/json". | Optional |
| contentBody | string | This is the content body that you would like to send when executing an "on" request. | Optional |
| contentBodyOff | string | This is the content body that you would like to send when executing an "off" request. | Optional |
Basic Example
{
"id" : 6789,
"name" : "bedroom light",
"deviceType" : "switch",
"onUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=41",
"offUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=41"
}
Response
{
"id" : 6789,
"name" : "table light",
"deviceType" : "switch",
"onUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=41",
"offUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=41"
}
Get all devices
Get all devices saved in the HA bridge configuration.
GET http://host:8080/api/devices
Response
Individual entries are the same as a single device but in json list format.
[{
"id" : 12345,
"name" : "bedroom light",
"deviceType" : "switch",
"onUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=41",
"offUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=41"
}
{
"id" : 6789,
"name" : "table light",
"deviceType" : "switch",
"onUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=41",
"offUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=41"
}]
Get a specific device
Get a device by ID assigned from creation and saved in the HA bridge configuration.
GET http://host:8080/api/devices/<id>
Response
The response is the same layout as defined in the add device response.
{
"id" : 6789,
"name" : "table light",
"deviceType" : "switch",
"onUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1&DeviceNum=41",
"offUrl" : "http://192.168.1.201:3480/data_request?id=action&output_format=json&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0&DeviceNum=41"
}
Delete a specific device
Delete a device by ID assigned from creation and saved in the HA bridge configuration.
DELETE http://host:8080/api/devices/<id>
Response
This call returns a null json "{}".
Get HA Bridge Version
Get current version of the HA bridge software.
GET http://host:8080/api/devices/habridge/version
Response
| Name | Type | Description |
|---|---|---|
| version | string | The version returned by the software. |
{"version":"1.0.7"}
Get Vera Devices
Get the list of devices available from the Vera, VeraLite or VeraEdge if configured. Please refer to the Luup Sdata Structure for the explanation of the devices list returned.
GET http://host:8080/api/devices/vera/devices
Response
| Name | Type | Description |
|---|---|---|
| name | string | The name of the Vera device. |
| altid | string | Vera internal alternate id |
| id | string | Vera id for accessing device. This is used in calls to the vera url for control. |
| category | string | Vera category name. |
| subcategory | string | Vera subcategory identifier. |
| room | string | Room name assigned to device. |
| parent | string | Vera id of the parent device. |
| status | string | Vera status for identifying on = 1 and off = 2. |
| level | string | Vera device dim percentage. |
| state | string | Vera additional state attribute. -1 means nothing is happening. 0, 1, 5, 6 mean that the engine is working. 2 or 3 indicates an error. 4 indicates a successful operation. |
| comment | string | Comment configured with device. Not always present. |
[{
"name":"Couch Left Lamp",
"altid":"4",
"id":"6",
"category":"Dimmable Light",
"subcategory":"0",
"room":"Family Room",
"parent":"1",
"status":"0",
"level":"0",
"state":"-1",
"comment":""
},
{
"name":"Couch Right Lamp",
"altid":"7",
"id":"9",
"category":"Dimmable Light",
"subcategory":"0",
"room":"Family Room",
"parent":"1",
"status":"0",
"level":"0",
"state":"-1",
"comment":""
}]
Get Vera Scenes
Get the list of scenes available from the Vera, VeraLite or VeraEdge if configured. Please refer to the Luup Sdata Structure for the explanation of the scenes list returned.
GET http://host:8080/api/devices/vera/scenes
Response
| Name | Type | Description |
|---|---|---|
| active | string | 1 if the scene is active, 0 otherwise. |
| name | string | The name of the Vera scene. |
| id | string | Vera id for accessing scene. This is used in calls to the vera url for control. |
| room | string | Room name assigned to scene. |
[{
"active":"1",
"name":"AccentLightsOff",
"id":"27",
"room":"no room"
},
{
"active":"0",
"name":"AccentLightsOn",
"id":"26",
"room":"no room"
}]
Get Harmony Activities
Get the list of activities available from the Harmony Hub if configured.
GET http://host:8080/api/devices/harmony/activities
Get Harmony Devices
Get the list of devices available from the Harmony Hub if configured.
GET http://host:8080/api/devices/harmony/devices
Show Harmony Current Activity
Show the Harmony Hub's current activity.
GET http://host:8080/api/devices/harmony/show
HUE REST API usage
This section will describe the REST api available for controlling the bridge based off of the HUE API. This Bridge does not support the full HUE API, only the calls that are supported with the HA Bridge are shown. The REST body examples are all formatted for easy reading, the actual body usage should be like this:
{"var1":"value1","var2":"value2","var3:"value3"}
Get all lights
Gets a list of all lights that have been discovered by the bridge.
GET http://host:port/api/<username>/lights
Response
Returns a list of all lights in the system.
If there are no lights in the system then the bridge will return an empty object, {}.
{
"1": {
"state": {
"on": true,
"bri": 144,
"hue": 0,
"sat": 254,
"xy": [0,0],
"ct": 0,
"alert": "none",
"effect": "none",
"colormode": "",
"reachable": true
},
"type": "Dimmable light",
"name": "Table Lamp 1",
"modelid": "LWB004",
"swversion": "65003148",
"pointsymbol": {
"1": "none",
"2": "none",
"3": "none",
"4": "none",
"5": "none",
"6": "none",
"7": "none",
"8": "none"
}
},
"2": {
"state": {
"on": false,
"bri": 0,
"hue": 0,
"sat": 0,
"xy": [0,0],
"ct": 0,
"alert": "none",
"effect": "none",
"colormode": "",
"reachable": true
},
"type": "Dimmable light",
"name": "Table Lamp 2",
"modelid": "LWB004",
"swversion": "65003148",
"pointsymbol": {
"1": "none",
"2": "none",
"3": "none",
"4": "none",
"5": "none",
"6": "none",
"7": "none",
"8": "none"
}
}
}
Get light attributes and state
Gets the attributes and state of a given light.
GET http://host:port/api/<username>/lights/<id>
Response
| Name | Type | Description |
|---|---|---|
| state | state object | Details the state of the light, see the state table below for more details. |
| type | string | A fixed name describing the type of light which will be "Dimmable light". |
| name | string 0, 32 | A unique, editable name given to the light. |
| modelid | string 6, 6 | The hardware model of the light which will be "LWB004". |
| uniqueid | string 6, 32 | Unique id of the device. |
| manufacturername | string 6, 32 | The manufacturer name will be "Philips". |
| luminaireuniqueid | string 6, 32 | This will be empty. |
| swversion | string 8, 8 | An identifier for the software version running on the light which will be "65003148". |
| Pointsymbol | object | This parameter is reserved for future functionality and returns a preset hashmap. |
The state object contains the following fields
| Name | Type | Description |
|---|---|---|
| on | bool | On/Off state of the light. On=true, Off=false |
| bri | uint8 | Brightness of the light. This will be set to 254 as a default. |
| hue | uint16 | This will be empty. |
| sat | uint8 | This will be set to 254. |
| xy | list 2..2 of float 4 | This will be empty. |
| ct | uint16 | This will be empty. |
| alert | string | This will be set to "none". |
| effect | string | This will be set to "none". |
| colormode | string 2, 2 | This will be empty. |
| reachable | bool | Indicates if a light can be reached by the bridge and will be set to true. |
{
"state": {
"hue": 0,
"on": true,
"effect": "none",
"alert": "none",
"bri": 254,
"sat": 254,
"ct": 0,
"xy": [0, 0],
"reachable": true,
"colormode": ""
},
"type": "Dimmable light",
"name": "Kitchen Ceiling",
"modelid": "LWB004",
"swversion": "65003148",
"pointsymbol": {
"1": "none",
"2": "none",
"3": "none",
"4": "none",
"5": "none",
"6": "none",
"7": "none",
"8": "none"
}
}
Set light state
Allows the user to turn the light on and off, modify the brightness.
PUT http://host:port/api/<username>/lights/<id>/state
Body arguments
| Name | Type | Description |
|---|---|---|
| on | bool | On/Off state of the light. On=true, Off=false. Optional |
| bri | uint8 | The brightness value to set the light to. Brightness is a scale from 1 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 1 is not off. e.g. "brightness": 60 will set the light to a specific brightness. Optional |
{
"on": true,
"bri": 200
}
Response
A response to a successful PUT request contains confirmation of the arguments passed in. Note: If the new value is too large to return in the response due to internal memory constraints then a value of "Updated." is returned.
[
{"success":{"/lights/1/state/bri":200}},
{"success":{"/lights/1/state/on":true}},
]
Create user
Emulates creating a new user. The link button state on the HA Bridge is always on for the purpose of responding to this request. No actual user is saved as this is for compatibility.
POST http://host:port/api
Body arguments
| Name | Type | Description | Required |
|---|---|---|---|
| devicetype | string 0..40 | <application_name># application_name string 0..20, devicename string 0..19 (Example: my_hue_app#iphone peter ) | Required |
| username | string 10..40 | A username. If this is not provided, a random key will be generated and returned in the response. | Optional |
{"devicetype": "my_hue_app#iphone peter"}
Response
Contains a list with a single item that details whether the user was added successfully along with the username parameter.
[{"success":{"username": "83b7780291a6ceffbe0bd049104df"}}]
Get full state (datastore)
This command is used to fetch the entire datastore from the device, including settings and state information for lights, groups, schedules and configuration.
GET http://host:port/api/<username>
Response
| Name | Type | Description |
|---|---|---|
| lights | object | A collection of all lights and their attributes. |
| groups | object | A collection of all groups and their attributes. This is empty. |
| config | object | All configuration settings. |
| schedules | object | A collection of all schedules and their attributes. This is not given. |
| scenes | object | A collection of all scenes and their attributes. This is empty. |
| sensors | object | A collection of all sensors and their attributes. This is not given. |
| rules | object | A collection of all rules and their attributes. This is not given. |
{
"lights": {
"1": {
"state": {
"on": false,
"bri": 0,
"hue": 0,
"sat": 254,
"xy": [0.0000, 0.0000],
"ct": 0,
"alert": "none",
"effect": "none",
"colormode": "",
"reachable": true
},
"type": "Dimmable light",
"name": "Table Lamp 1",
"modelid": "LWB004",
"swversion": "65003148",
"pointsymbol": {
"1": "none",
"2": "none",
"3": "none",
"4": "none",
"5": "none",
"6": "none",
"7": "none",
"8": "none"
}
},
"2": {
"state": {
"on": true,
"bri": 254,
"hue": 0,
"sat": 254,
"xy": [0, 0],
"ct": 0,
"alert": "none",
"effect": "none",
"colormode": "",
"reachable": true
},
"type": "Dimmable light",
"name": "Table Lamp 2",
"modelid": "LWB004",
"swversion": "65003148",
"pointsymbol": {
"1": "none",
"2": "none",
"3": "none",
"4": "none",
"5": "none",
"6": "none",
"7": "none",
"8": "none"
}
}
},
"scenes":{
},
"groups":{
},
"config": {
"name": "Philips hue",
"mac": "00:00:88:00:bb:ee",
"dhcp": true,
"ipaddress": "192.168.1.74",
"netmask": "255.255.255.0",
"gateway": "192.168.1.254",
"proxyaddress": "",
"proxyport": 0,
"UTC": "2012-10-29T12:00:00",
"whitelist": {
"1028d66426293e821ecfd9ef1a0731df": {
"last use date": "2012-10-29T12:00:00",
"create date": "2012-10-29T12:00:00",
"name": "test user"
}
},
"swversion": "01003372",
"swupdate": {
"updatestate": 0,
"url": "",
"text": "",
"notify": false
},
"linkbutton": false,
"portalservices": false
},
}
Ask Alexa
After this Tell Alexa: "Alexa, discover my devices"
Then you can say "Alexa, Turn on the office light" or whatever name you have given your configured devices.
To view or remove devices that Alexa knows about, you can use the mobile app Menu / Settings / Connected Home
Debugging
To turn on debugging for the bridge, use the following extra parm in the command line:
-Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG
Development Mode
To turn on development mode so that it will not need an Harmony Hub for testing, use the following extra parm in the command line and the harmony ip and login info will not be needed:
java -jar -Ddev.mode=true ha-bridge-0.X.Y.jar