mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 03:15:14 -06:00
add LICENSE and README
types.go -> eventTypes.go reorganize example folder structure
This commit is contained in:
74
example/example.go
Normal file
74
example/example.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
ga "github.com/saml-dev/gome-assistant"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := ga.NewApp("0.0.0.0:8123") // Replace with your Home Assistant IP Address
|
||||
defer app.Cleanup()
|
||||
|
||||
pantryDoor := ga.
|
||||
EntityListenerBuilder().
|
||||
EntityIds("binary_sensor.pantry_door").
|
||||
Call(pantryLights).
|
||||
Build()
|
||||
|
||||
_11pmSched := ga.
|
||||
ScheduleBuilder().
|
||||
Call(lightsOut).
|
||||
Daily().
|
||||
At("23:00").
|
||||
Build()
|
||||
|
||||
zwaveEventListener := ga.
|
||||
EventListenerBuilder().
|
||||
EventTypes("zwave_js_value_notification").
|
||||
Call(onEvent).
|
||||
Build()
|
||||
|
||||
app.RegisterEntityListener(pantryDoor)
|
||||
app.RegisterSchedule(_11pmSched)
|
||||
app.RegisterEventListener(zwaveEventListener)
|
||||
|
||||
app.Start()
|
||||
|
||||
}
|
||||
|
||||
func pantryLights(service *ga.Service, sensor ga.EntityData) {
|
||||
l := "light.pantry"
|
||||
if sensor.ToState == "on" {
|
||||
service.HomeAssistant.TurnOn(l)
|
||||
} else {
|
||||
service.HomeAssistant.TurnOff(l)
|
||||
}
|
||||
}
|
||||
|
||||
func onEvent(service *ga.Service, data ga.EventData) {
|
||||
// Since the structure of the event changes depending
|
||||
// on the event type, you can Unmarshal the raw json
|
||||
// into a Go type. If a type for your event doesn't
|
||||
// exist, you can write it yourself! PR's welcome to
|
||||
// the eventTypes.go file :)
|
||||
ev := ga.EventZWaveJSValueNotification{}
|
||||
json.Unmarshal(data.RawEventJSON, &ev)
|
||||
log.Default().Println(ev)
|
||||
}
|
||||
|
||||
func lightsOut(service *ga.Service, state *ga.State) {
|
||||
service.Light.TurnOff("light.outside_lights")
|
||||
s, err := state.Get("binary_sensor.living_room_motion")
|
||||
if err != nil {
|
||||
log.Default().Println("couldnt get living room motion state, doing nothing")
|
||||
return
|
||||
}
|
||||
|
||||
// if no motion detected in living room for 30mins
|
||||
if s.State == "off" && time.Now().Sub(s.LastChanged).Minutes() > 30 {
|
||||
service.Light.TurnOff("light.main_lights")
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
ga "github.com/saml-dev/gome-assistant"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := ga.NewApp("192.168.86.67:8123")
|
||||
defer app.Cleanup()
|
||||
pantryDoor := ga.
|
||||
EntityListenerBuilder().
|
||||
EntityIds("binary_sensor.pantry_door").
|
||||
Call(pantryLights).
|
||||
Build()
|
||||
zwaveEventListener := ga.
|
||||
EventListenerBuilder().
|
||||
EventType("zwave_js_value_notification").
|
||||
Call(onEvent).
|
||||
Build()
|
||||
app.RegisterEntityListener(pantryDoor)
|
||||
app.RegisterSchedule(ga.ScheduleBuilder().Call(cool).Every("5s").Build())
|
||||
app.RegisterEventListener(zwaveEventListener)
|
||||
|
||||
app.Start()
|
||||
|
||||
}
|
||||
|
||||
func pantryLights(service *ga.Service, data ga.EntityData) {
|
||||
l := "group.kitchen_ceiling_lights"
|
||||
// service.HomeAssistant.Toggle("group.living_room_lamps", map[string]any{"brightness_pct": 100})
|
||||
// service.Light.Toggle("light.entryway_lamp", map[string]any{"brightness_pct": 100})
|
||||
if data.ToState == "on" {
|
||||
service.HomeAssistant.TurnOn(l)
|
||||
} else {
|
||||
service.HomeAssistant.TurnOff(l)
|
||||
}
|
||||
}
|
||||
|
||||
func onEvent(service *ga.Service, data ga.EventData) {
|
||||
// service.HomeAssistant.Toggle("light.el_gato_key_lights")
|
||||
ev := ga.EventZWaveJSValueNotification{}
|
||||
json.Unmarshal(data.RawEventJSON, &ev)
|
||||
log.Default().Println(ev)
|
||||
}
|
||||
|
||||
func cool(service *ga.Service, state *ga.State) {
|
||||
// service.InputDatetime.Set("input_datetime.garage_last_triggered_ts", time.Now())
|
||||
// service.Light.TurnOn("light.entryway_lamp")
|
||||
// log.Default().Println("B")
|
||||
}
|
||||
|
||||
func c(service *ga.Service, state *ga.State) {
|
||||
// log.Default().Println("C")
|
||||
}
|
||||
|
||||
func listenerCB(service *ga.Service, data ga.EntityData) {
|
||||
log.Default().Println("hi")
|
||||
}
|
||||
Reference in New Issue
Block a user