mirror of
https://github.com/Xevion/go-ha.git
synced 2025-12-06 01:15:10 -06:00
Added check of entity state change callback.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
app:
|
hass:
|
||||||
ip_address: 192.168.0.204
|
address: 192.168.0.204
|
||||||
port: 8123
|
port: 8123
|
||||||
home_zone_entity_id: zone.home
|
zone: zone.home
|
||||||
|
|
||||||
entities:
|
entities:
|
||||||
light_entity_id: light.office_bed_lamp
|
light_entity_id: light.office_desk_lamp
|
||||||
|
|||||||
@@ -15,16 +15,17 @@ import (
|
|||||||
type (
|
type (
|
||||||
MySuite struct {
|
MySuite struct {
|
||||||
suite.Suite
|
suite.Suite
|
||||||
app *ga.App
|
app *ga.App
|
||||||
config *Config
|
config *Config
|
||||||
|
suiteCtx map[string]any
|
||||||
}
|
}
|
||||||
|
|
||||||
Config struct {
|
Config struct {
|
||||||
App struct {
|
Hass struct {
|
||||||
HAAuthToken string `yaml:"ha_auth_token"`
|
HAAuthToken string `yaml:"token"`
|
||||||
IpAddress string `yaml:"ip_address"`
|
IpAddress string `yaml:"address"`
|
||||||
Port string `yaml:"port"`
|
Port string `yaml:"port"`
|
||||||
HomeZoneEntityId string `yaml:"home_zone_entity_id"`
|
HomeZoneEntityId string `yaml:"zone"`
|
||||||
}
|
}
|
||||||
Entities struct {
|
Entities struct {
|
||||||
LightEntityId string `yaml:"light_entity_id"`
|
LightEntityId string `yaml:"light_entity_id"`
|
||||||
@@ -32,55 +33,78 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *MySuite) SetupTest() {
|
func (s *MySuite) SetupSuite() {
|
||||||
|
s.suiteCtx = make(map[string]any)
|
||||||
|
|
||||||
configFile, err := os.ReadFile("./config.yaml")
|
configFile, err := os.ReadFile("./config.yaml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Error reading config file", err)
|
slog.Error("Error reading config file", err)
|
||||||
}
|
}
|
||||||
s.config = &Config{}
|
s.config = &Config{}
|
||||||
// either env var or config file can be used to set HA auth. token
|
// either env var or config file can be used to set HA auth. token
|
||||||
s.config.App.HAAuthToken = os.Getenv("HA_AUTH_TOKEN")
|
s.config.Hass.HAAuthToken = os.Getenv("HA_AUTH_TOKEN")
|
||||||
if err := yaml.Unmarshal(configFile, s.config); err != nil {
|
if err := yaml.Unmarshal(configFile, s.config); err != nil {
|
||||||
slog.Error("Error unmarshalling config file:", err)
|
slog.Error("Error unmarshalling config file:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.app, err = ga.NewApp(ga.NewAppRequest{
|
s.app, err = ga.NewApp(ga.NewAppRequest{
|
||||||
HAAuthToken: s.config.App.HAAuthToken,
|
HAAuthToken: s.config.Hass.HAAuthToken,
|
||||||
IpAddress: s.config.App.IpAddress,
|
IpAddress: s.config.Hass.IpAddress,
|
||||||
HomeZoneEntityId: s.config.App.HomeZoneEntityId,
|
HomeZoneEntityId: s.config.Hass.HomeZoneEntityId,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Failed to createw new app:", err)
|
slog.Error("Failed to createw new app:", err)
|
||||||
s.T().FailNow()
|
s.T().FailNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
entityId := s.config.Entities.LightEntityId
|
||||||
|
if entityId != "" {
|
||||||
|
s.suiteCtx["entityCallbackInvoked"] = false
|
||||||
|
etl := ga.NewEntityListener().EntityIds(entityId).Call(s.entityCallback).Build()
|
||||||
|
s.app.RegisterEntityListeners(etl)
|
||||||
|
go s.app.Start()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MySuite) TearDownSuite() {
|
func (s *MySuite) TearDownSuite() {
|
||||||
if s.app != nil {
|
if s.app != nil {
|
||||||
s.app.Cleanup()
|
s.app.Cleanup()
|
||||||
|
s.app = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Basic test of ga app creation and light toggle service
|
// Basic test of light toggle service and entity listener
|
||||||
func (s *MySuite) TestLightService() {
|
func (s *MySuite) TestLightService() {
|
||||||
entityId := s.config.Entities.LightEntityId
|
entityId := s.config.Entities.LightEntityId
|
||||||
|
|
||||||
initialState, err := s.app.GetState().Get(entityId)
|
if entityId != "" {
|
||||||
|
initState := getEntityState(s, entityId)
|
||||||
|
s.app.GetService().Light.Toggle(entityId)
|
||||||
|
|
||||||
|
assert.EventuallyWithT(s.T(), func(c *assert.CollectT) {
|
||||||
|
newState := getEntityState(s, entityId)
|
||||||
|
assert.NotEqual(c, initState, newState)
|
||||||
|
assert.True(c, s.suiteCtx["entityCallbackInvoked"].(bool))
|
||||||
|
}, 10*time.Second, 1*time.Second, "State of light entity did not change or callback was invoked")
|
||||||
|
} else {
|
||||||
|
s.T().Skip("No light entity id provided")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test if event has been captured after light entity state changed
|
||||||
|
func (s *MySuite) entityCallback(se *ga.Service, st ga.State, e ga.EntityData) {
|
||||||
|
slog.Info("Entity callback called.", "entity id:", e.TriggerEntityId, "from state:", e.FromState, "to state:", e.ToState)
|
||||||
|
s.suiteCtx["entityCallbackInvoked"] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEntityState(s *MySuite, entityId string) string {
|
||||||
|
state, err := s.app.GetState().Get(entityId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Error getting entity state:", err)
|
slog.Error("Error getting entity state:", err)
|
||||||
|
s.T().FailNow()
|
||||||
}
|
}
|
||||||
slog.Info("Initial state of entity:", "state", initialState.State)
|
slog.Info("State of entity:", "state", state.State)
|
||||||
|
return state.State
|
||||||
s.app.GetService().Light.Toggle(entityId)
|
|
||||||
|
|
||||||
time.Sleep(1 * time.Second) // wait for state to update
|
|
||||||
|
|
||||||
newState, err := s.app.GetState().Get(entityId)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("Error getting entity state:", err)
|
|
||||||
}
|
|
||||||
slog.Info("New state of entity:", "state", newState.State)
|
|
||||||
assert.NotEqual(s.T(), initialState.State, newState.State)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the test suite
|
// Run the test suite
|
||||||
|
|||||||
Reference in New Issue
Block a user