mirror of
https://github.com/Xevion/HATray.git
synced 2025-12-06 01:15:11 -06:00
feat: setup dirty demo of icon setting & sensor listening
This commit is contained in:
@@ -132,15 +132,29 @@ func (app *App) Resume() error {
|
|||||||
URL: app.config.Server,
|
URL: app.config.Server,
|
||||||
HAAuthToken: app.config.APIKey,
|
HAAuthToken: app.config.APIKey,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.logger.Error("failed to create Home Assistant app", "error", err)
|
app.logger.Error("failed to create Home Assistant app", "error", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
app.ha.Cleanup()
|
app.ha.RegisterEntityListeners(ga.NewEntityListener().EntityIds("binary_sensor.bedroom_door_opening").Call(app.onEntityStateChange).Build())
|
||||||
|
|
||||||
app.tray.SetIcon(IconUnknown)
|
go app.ha.Start()
|
||||||
|
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
state, err := app.ha.GetState().Get("binary_sensor.bedroom_door_opening")
|
||||||
|
if err != nil {
|
||||||
|
app.logger.Error("failed to get entity", "error", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
app.logger.Info("state", "state", state.State)
|
||||||
|
|
||||||
|
if state.State == "on" {
|
||||||
|
app.tray.SetIcon(IconOpen)
|
||||||
|
} else {
|
||||||
|
app.tray.SetIcon(IconClosed)
|
||||||
|
}
|
||||||
|
|
||||||
app.state = StateRunning
|
app.state = StateRunning
|
||||||
app.lastStarted = internal.Ptr(time.Now())
|
app.lastStarted = internal.Ptr(time.Now())
|
||||||
@@ -152,6 +166,21 @@ func (app *App) Resume() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *App) onEntityStateChange(se *ga.Service, st ga.State, e ga.EntityData) {
|
||||||
|
entity, err := st.Get(e.TriggerEntityId)
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Error("failed to get entity", "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
a.logger.Info("sensor.test state changed", "entity", e.TriggerEntityId, "state", entity.State)
|
||||||
|
|
||||||
|
if entity.State == "on" {
|
||||||
|
a.tray.SetIcon(IconOpen)
|
||||||
|
} else {
|
||||||
|
a.tray.SetIcon(IconClosed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Reload pauses the application, re-reads configuration files, then resumes
|
// Reload pauses the application, re-reads configuration files, then resumes
|
||||||
func (a *App) Reload() error {
|
func (a *App) Reload() error {
|
||||||
a.mu.Lock()
|
a.mu.Lock()
|
||||||
|
|||||||
Reference in New Issue
Block a user