Skip to content

Commit

Permalink
Updates README, change config file from json to yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Perlet committed Apr 23, 2019
1 parent 6bf20c7 commit 61ba29a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 54 deletions.
92 changes: 54 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@ Usage: ./huevent [OPTIONS]
```

## Supported Device and Event Types

| Device | Event-Type (eventType) | Event-Data (triggerOn) | Description |
| ----------------- | ---------------------- | ---------------------- | ------------------------------------------------------------- |
| Hue Tap | buttonevent | 34 | 1 Dot Key |
| | buttonevent | 16 | 2 Dot Key |
| | buttonevent | 17 | 3 Dot Key |
| | buttonevent | 18 | 4 Dot Key |
| Hue Dimmer Switch | buttonevent | 1000 | Hard press ON, followed by Event-Data 1002 |
| | buttonevent | 1001 | Long press ON (send while hold the button) |
| | buttonevent | 1003 | Release ON (after Long press) |
| | buttonevent | 1002 | Soft press ON |
| | buttonevent | 2000 | Hard press BRIGHTER, followed by Event-Data 2002 |
| | buttonevent | 2001 | Long press BRIGHTER (send while hold the button) |
| | buttonevent | 2003 | Release BRIGHTER |
| | buttonevent | 2002 | Soft press BRIGHTER |
| | buttonevent | 3000 | Hard press DARKER, followed by Event-Data 3002 |
| | buttonevent | 3001 | Long press DARKER |
| | buttonevent | 3003 | Release DARKER |
| | buttonevent | 3002 | Soft press DARKER |
| | buttonevent | 4000 | Hard press OFF, followed by Event-Data 4002 |
| | buttonevent | 4001 | Long press OFF |
| | buttonevent | 4003 | Release OFF |
| | buttonevent | 4002 | Soft press OFF |
| Hue Motion Sensor | presence | true | Motion detected |
| | presence | false | No Motion detected |
| | temperature | xxxx | Temperature in °C multiplied by 100 (2655 == 26.55°C) |
| | lightlevel | xxxxx | Lightlevel (0 ~ dark, 20000 ~ normal, 40000 ~ very bright) |



## Build
Expand Down Expand Up @@ -61,46 +90,33 @@ echo "$BRIDGE_IP $USERNAME"


```
{
"config": {
"ip": "192.x.x.x",
"user": "nEhN3DMvjWBr....."
},
"hooks": [
{
"deviceId": "00:17:88:01:10:33:35:98-02-fc00",
"eventType": "buttonevent",
"keyCode": "1002",
"cmd": "echo Button pressed"
},
{
"deviceId": "00:00:00:00:00:42:43:2f-f2",
"eventType": "buttonevent",
"keyCode": "18",
"cmd": "echo Execute with payload $HUEVENT_PAYLOAD "
},
{
"deviceId": "00:17:88:01:03:29:57:55-02-0406",
"eventType": "presence",
"keyCode": "true",
"cmd": "echo SOMEBODY BECOMES PRESENT"
},
{
"deviceId": "00:17:88:01:03:29:57:55-02-0406",
"eventType": "presence",
"keyCode": "false",
"cmd": "echo SOMEBODY IS ABSENT"
},
{
"deviceId": "00:17:88:01:03:29:57:55-02-0402",
"eventType": "temperature",
"cmd": "python -c 'import os; print(str(float(os.environ[\"HUEVENT_PAYLOAD\"])/100.0) + \"°C\")'"
}
],
"deviceFilter": []
}
config:
ip: 192.168....
user: nEh...
hooks:
- deviceId: 00:17:88:01:10:33:35:98-02-fc00
eventType: buttonevent
triggerOn: 1002
cmd: echo echo
- deviceId: 00:00:00:00:00:42:43:2f-f2
eventType: buttonevent
triggerOn: 18
cmd: echo $HUEVENT_PAYLOAD NOOOOPE
- deviceId: 00:17:88:01:03:29:57:55-02-0406
eventType: presence
triggerOn: 'true'
cmd: echo SOMEBODY BECOMES PRESENT
- deviceId: 00:17:88:01:03:29:57:55-02-0406
eventType: presence
triggerOn: 'false'
cmd: echo SOMEBODY IS ABSENT
- deviceId: 00:17:88:01:03:29:57:55-02-0402
eventType: temperature
cmd: python -c 'import os; print(str(float(os.environ["HUEVENT_PAYLOAD"])/100.0) + "°C")'
deviceFilter: []
```


## Examples

* A Remote for Fritz Dect200 Power-Sockets
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/mperlet/huevent

go 1.12

require gopkg.in/yaml.v2 v2.2.2 // indirect
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
38 changes: 22 additions & 16 deletions huevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path"
"time"
"gopkg.in/yaml.v2"
)

type config struct {
Expand All @@ -28,18 +30,18 @@ type config struct {

type HueventConfig struct {
Config struct {
Ip string `json:"ip"`
User string `json:"user"`
} `json:"config"`
Hooks []Hook `json:"hooks"`
DeviceFilter []string `json:"deviceFilter"`
Ip string `yaml:"ip"`
User string `yaml:"user"`
} `yaml:"config"`
Hooks []Hook `yaml:"hooks"`
DeviceFilter []string `yaml:"deviceFilter"`
}

type Hook struct {
DeviceID string `json:"deviceId"`
EventType string `json:"eventType"`
KeyCode string `json:"keyCode,omitempty"`
Cmd string `json:"cmd"`
DeviceID string `yaml:"deviceId"`
EventType string `yaml:"eventType"`
TriggerOn string `yaml:"triggerOn,omitempty"`
Cmd string `yaml:"cmd"`
}

type hueBridgeResponse struct {
Expand Down Expand Up @@ -229,18 +231,18 @@ func poll(conf *config) {
}
}

func exit(device string, eventType string, keyCode string, conf *config) {
fmt.Printf("%s\t%s\t%s\n", device, eventType, keyCode)
func exit(device string, eventType string, triggerOn string, conf *config) {
fmt.Printf("%s\t%s\t%s\n", device, eventType, triggerOn)

for _, hook := range *conf.hooks {

if hook.DeviceID != device && hook.EventType != eventType {
continue
}

if hook.KeyCode == "" || hook.KeyCode == keyCode {
if hook.TriggerOn == "" || hook.TriggerOn == triggerOn {
//noinspection ALL
go executeCommand(hook.Cmd, device, eventType, keyCode)
go executeCommand(hook.Cmd, device, eventType, triggerOn)
}

}
Expand Down Expand Up @@ -353,7 +355,7 @@ func PathExists(path string) bool {

func configPath() string {
var configDirectory = path.Join(os.Getenv("HOME"), ".huevent")
return path.Join(configDirectory, "config.json")
return path.Join(configDirectory, "huevent.yml")
}

func readConfig(filepath string) HueventConfig {
Expand All @@ -365,15 +367,19 @@ func readConfig(filepath string) HueventConfig {
writeConfig(HueventConfig{}, filepath)
}

unmarshalErr := json.Unmarshal(content, &hueventConfig)
unmarshalErr := yaml.Unmarshal(content, &hueventConfig)
if unmarshalErr != nil {
fmt.Printf("Can't parse config file (%s), delete it and create a new with -pair argument", filepath)
}
return hueventConfig
}

func writeConfig(config HueventConfig, filepath string) {
var a, _ = json.MarshalIndent(config, "", " ")

var a, err = yaml.Marshal(&config)
if err != nil {
log.Fatalf("error: %v", err)
}

if DEBUG {
fmt.Printf("Write Config %s \n %s \n", filepath, a)
Expand Down

0 comments on commit 61ba29a

Please sign in to comment.