From b92147a6a643bd2297cf1596a48266124ced387f Mon Sep 17 00:00:00 2001 From: Sam Lewis Date: Mon, 17 Oct 2022 12:32:26 -0400 Subject: [PATCH] remove unnecessary check from throttle check --- entitylistener.go | 5 ++--- example/main/testing.go | 28 +++++++++++----------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/entitylistener.go b/entitylistener.go index 758cbcb..1c8b7e9 100644 --- a/entitylistener.go +++ b/entitylistener.go @@ -178,9 +178,8 @@ func callEntityListeners(app *app, msgBytes []byte) { } // don't run callback if Throttle is set and that duration hasn't passed since lastRan - if l.throttle.Seconds() > 0 && // throttle is set - !l.lastRan.Eq(carbon.Now().StartOfCentury()) && // lastRan is set aka this callback has been called since starting gomeassistant - l.lastRan.DiffAbsInSeconds(carbon.Now()) < int64(l.throttle.Seconds()) { // it's been less than seconds since it last ran + if l.throttle.Seconds() > 0 && + l.lastRan.DiffAbsInSeconds(carbon.Now()) < int64(l.throttle.Seconds()) { return } diff --git a/example/main/testing.go b/example/main/testing.go index 13e22ed..09b2e1c 100644 --- a/example/main/testing.go +++ b/example/main/testing.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" ga "github.com/saml-dev/gome-assistant" @@ -10,31 +9,26 @@ import ( func main() { app := ga.NewApp("192.168.86.67:8123") defer app.Cleanup() - s := ga.ScheduleBuilder().Call(lightsOut).Daily().At(app.Sunset("1h")).Build() - s2 := ga.ScheduleBuilder().Call(lightsOut).Every("2h").Offset("10m").Build() - fmt.Println(s2) - app.RegisterSchedule(s) - app.RegisterSchedule(s2) - simpleListener := ga.EntityListenerBuilder(). - EntityIds("group.office_ceiling_lights"). - Call(listenerCB). - OnlyAfter("23:03"). - // Throttle("5s"). + pantryDoor := ga. + EntityListenerBuilder(). + EntityIds("binary_sensor.pantry_door"). + Call(pantryLights). Build() - app.RegisterEntityListener(simpleListener) + app.RegisterEntityListener(pantryDoor) app.Start() } -func lightsOut(service *ga.Service, state *ga.State) { +func pantryLights(service *ga.Service, data ga.EntityData) { // service.InputDatetime.Set("input_datetime.garage_last_triggered_ts", time.Now()) // 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}) - service.HomeAssistant.Toggle("light.entryway_lamp") - log.Default().Println("running lightsOut") - // service.HomeAssistant.Toggle("light.entryway_lamp") - // log.Default().Println("A") + if data.ToState == "on" { + service.HomeAssistant.TurnOn("switch.pantry_light_2") + } else { + service.HomeAssistant.TurnOff("switch.pantry_light_2") + } } func cool(service *ga.Service, state *ga.State) {