Skip to content

Commit

Permalink
Merge pull request #5 from stjohnjohnson/ImprovingBridge
Browse files Browse the repository at this point in the history
Adding a whole bunch of updates
  • Loading branch information
stjohnjohnson committed Feb 6, 2016
2 parents 6129895 + 8b1a43a commit 3bb9608
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ $ mqtt pub -t '/SmartThings/Fireplace Lights/switch' -m 'off'

# Configuration

The bridge has one yaml file for configuration. Currently we only have one item you can set:
The bridge has one yaml file for configuration. Currently we only have one item you can set:

```
---
Expand All @@ -52,7 +52,7 @@ We'll be adding additional fields as this service progresses (port, username, pa
1. Run the Docker container

```
$ docker run \
$ docker run \
-d \
--name="mqtt-bridge" \
-v /opt/mqtt-bridge:/config \
Expand All @@ -71,9 +71,42 @@ We'll be adding additional fields as this service progresses (port, username, pa
6. Configure the Smart App (via the Native App) with the devices you want to share and the Device Handler you just installed as the bridge
7. Watch as MQTT is populated with events from your devices
## Docker Compose
If you want to bundle everything together, you can use [Docker Compose][docker-compose].
Just create a file called `docker-compose.yml` with this contents:
```yaml
mqtt:
image: matteocollina/mosca
ports:
- 1883:1883
mqttbridge:
image: stjohnjohnson/smartthings-mqtt-bridge
volumes:
- ./mqtt-bridge:/config
ports:
- 8080:8080
links:
- mqtt
homeassistant:
image: balloob/home-assistant
ports:
- 80:80
volumes:
- ./home-assistant:/config
- /etc/localtime:/etc/localtime:ro
links:
- mqtt
```

This creates a directory called `./mqtt-bridge/` to store configuration for the bridge. It also creates a directory `./home-assistant` to store configuration for HA.

[dt]: https://github.com/stjohnjohnson/smartthings-mqtt-bridge/blob/master/devicetypes/stj/mqtt-bridge.src/mqtt-bridge.groovy
[app]: https://github.com/stjohnjohnson/smartthings-mqtt-bridge/blob/master/smartapps/stj/mqtt-bridge.src/mqtt-bridge.groovy
[ide-dt]: https://graph.api.smartthings.com/ide/devices
[ide-app]: https://graph.api.smartthings.com/ide/apps
[ha-issue]: https://github.com/balloob/home-assistant/issues/604
[docker-compose]: https://docs.docker.com/compose/
3 changes: 2 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
mqtt:
host: localhost
# Specify your MQTT Broker's hostname or IP address here
host: mqtt
13 changes: 11 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ var config = loadConfiguration(),
subscription,
history = {};

// Write all events to disk as well
winston.add(winston.transports.File, {
filename: path.join(CONFIG_DIR, 'events.log'),
json: false
});

/**
* Load user configuration (or create it)
* @method loadConfiguration
Expand Down Expand Up @@ -139,8 +145,11 @@ function parseMQTTMessage (topic, message) {
value: message.toString()
}
}, function (error, resp) {
// @TODO handle the response from SmartThings
winston.log(error, resp.statusCode);
if (error) {
// @TODO handle the response from SmartThings
winston.error('Error from SmartThings Hub: %s', error.toString());
winston.error(JSON.stringify(resp, null, 4));
}
});
}

Expand Down
25 changes: 20 additions & 5 deletions smartapps/stj/mqtt-bridge.src/mqtt-bridge.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ definition(

preferences {
section ("Input") {
input "switches", "capability.switch", title: "Switches", multiple: true
input "levels", "capability.switchLevel", title: "Levels", multiple: true
input "powerMeters", "capability.powerMeter", title: "Power Meters", multiple: true
input "switches", "capability.switch", title: "Switches", multiple: true, required: false
input "levels", "capability.switchLevel", title: "Levels", multiple: true, required: false
input "powerMeters", "capability.powerMeter", title: "Power Meters", multiple: true, required: false
input "motionSensors", "capability.motionSensor", title: "Motion Sensors", multiple: true, required: false
}

section ("Bridge") {
Expand All @@ -45,13 +46,16 @@ preferences {
def installed() {
log.debug "Installed with settings: ${settings}"

runEvery15Minutes(initialize)
initialize()
}

def updated() {
log.debug "Updated with settings: ${settings}"

// Unsubscribe from all events
unsubscribe()
// Subscribe to stuff
initialize()
}

Expand All @@ -65,18 +69,27 @@ def getDeviceNames(devices) {
}

def initialize() {
// Subscribe to new events from devices
subscribe(powerMeters, "power", inputHandler)
subscribe(motionSensors, "motion", inputHandler)
subscribe(switches, "switch", inputHandler)
subscribe(levels, "level", inputHandler)

// Subscribe to events from the bridge
subscribe(bridge, "message", bridgeHandler)

// Updating subscription
// Update the bridge
updateSubscription()
}

// Update the bridge's subscription
def updateSubscription() {
def json = new groovy.json.JsonOutput().toJson([
path: '/subscribe',
body: [
devices: [
power: getDeviceNames(powerMeters),
motion: getDeviceNames(motionSensors),
switch: getDeviceNames(switches),
level: getDeviceNames(levels)
]
Expand All @@ -88,11 +101,13 @@ def initialize() {
bridge.deviceNotification(json)
}

// Receive an event from the bridge
def bridgeHandler(evt) {
def json = new JsonSlurper().parseText(evt.value)

switch (json.type) {
case "power":
case "motion":
// Do nothing, we can change nothing here
break
case "switch":
Expand All @@ -118,6 +133,7 @@ def bridgeHandler(evt) {
log.debug "Receiving device event from bridge: ${json}"
}

// Receive an event from a device
def inputHandler(evt) {
def json = new JsonOutput().toJson([
path: '/push',
Expand All @@ -129,6 +145,5 @@ def inputHandler(evt) {
])

log.debug "Forwarding device event to bridge: ${json}"

bridge.deviceNotification(json)
}

0 comments on commit 3bb9608

Please sign in to comment.