-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Dimmed lights can be re-triggered by sensor
- Loading branch information
Showing
10 changed files
with
222 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
cover.out | ||
sqlite.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package halautomations_test | ||
|
||
import ( | ||
"log/slog" | ||
"testing" | ||
"time" | ||
|
||
"github.com/dansimau/hal" | ||
halautomations "github.com/dansimau/hal/automations" | ||
"github.com/dansimau/hal/homeassistant" | ||
"github.com/dansimau/hal/testutil" | ||
"github.com/davecgh/go-spew/spew" | ||
) | ||
|
||
func TestSensorLightsTurnOnAfterDimming(t *testing.T) { | ||
t.Parallel() | ||
|
||
conn, server, cleanup := testutil.NewClientServer(t) | ||
defer cleanup() | ||
|
||
// Create test light | ||
testLight := hal.NewLight("test.light") | ||
conn.RegisterEntities(testLight) | ||
|
||
// Create test sensor | ||
testSensor := hal.NewBinarySensor("test.sensor") | ||
conn.RegisterEntities(testSensor) | ||
|
||
// Create automation | ||
automation := halautomations.NewSensorsTriggerLights(). | ||
WithName("test automation"). | ||
WithSensors(testSensor). | ||
WithLights(testLight). | ||
WithBrightness(100). | ||
TurnsOffAfter(time.Second * 3). | ||
DimLightsBeforeTurnOff(time.Second) | ||
|
||
conn.RegisterAutomations(automation) | ||
|
||
// Trigger motion sensor | ||
slog.Info("Test: Triggering motion sensor") | ||
server.SendStateChangeEvent(homeassistant.Event{ | ||
EventData: homeassistant.EventData{ | ||
EntityID: testSensor.GetID(), | ||
NewState: &homeassistant.State{ | ||
EntityID: testSensor.GetID(), | ||
State: "on", | ||
}, | ||
}, | ||
}) | ||
|
||
slog.Info("Test: Asserting light was turned on") | ||
testutil.WaitFor(t, "verify light was turned on", func() bool { | ||
return testLight.GetState().State == "on" | ||
}, func() { | ||
spew.Dump(testLight.GetID(), testLight.GetState()) | ||
}) | ||
|
||
// Clear motion sensor | ||
slog.Info("Test: Clearing motion sensor") | ||
server.SendStateChangeEvent(homeassistant.Event{ | ||
EventData: homeassistant.EventData{ | ||
EntityID: testSensor.GetID(), | ||
NewState: &homeassistant.State{ | ||
EntityID: testSensor.GetID(), | ||
State: "off", | ||
}, | ||
}, | ||
}) | ||
|
||
// TODO: Replace this with mocked time | ||
slog.Info("Test: Sleeping") | ||
time.Sleep(time.Second) | ||
|
||
slog.Info("Test: Asserting light was dimmed") | ||
testutil.WaitFor(t, "verify light was dimmed", func() bool { | ||
return testLight.GetState().Attributes["brightness"] == 50.0 | ||
}, func() { | ||
spew.Dump(testLight.GetID(), testLight.GetState(), testLight.GetState().Attributes["brightness"]) | ||
}) | ||
|
||
// Trigger motion sensor again | ||
slog.Info("Test: Triggering motion sensor again") | ||
server.SendStateChangeEvent(homeassistant.Event{ | ||
EventData: homeassistant.EventData{ | ||
EntityID: testSensor.GetID(), | ||
NewState: &homeassistant.State{ | ||
EntityID: testSensor.GetID(), | ||
State: "on", | ||
}, | ||
}, | ||
}) | ||
|
||
// Verify light is bright again | ||
slog.Info("Test: Asserting light is bright again") | ||
testutil.WaitFor(t, "verify light is bright again", func() bool { | ||
return testLight.GetState().Attributes["brightness"] == 100.0 | ||
}, func() { | ||
spew.Dump(testLight.GetID(), testLight.GetState()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.