parent
ddf56ec6aa
commit
06d87ea689
|
@ -2,6 +2,7 @@ package nats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.jhot.me/jhot/hats/pkg/config"
|
"code.jhot.me/jhot/hats/pkg/config"
|
||||||
|
@ -26,13 +27,12 @@ type HatsTimer struct {
|
||||||
FinishTime time.Time
|
FinishTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HatsTimer) getEntityId() string {
|
|
||||||
return fmt.Sprintf("timer.%s", t.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetTimer(name string) (*HatsTimer, error) {
|
func GetTimer(name string) (*HatsTimer, error) {
|
||||||
initHomeAssistantClient()
|
initHomeAssistantClient()
|
||||||
state, err := haClient.GetState(fmt.Sprintf("timer.%s", name))
|
if !strings.HasPrefix(name, "timer.") {
|
||||||
|
name = fmt.Sprintf("timer.%s", name)
|
||||||
|
}
|
||||||
|
state, err := haClient.GetState(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ func (t *HatsTimer) Activate(durationOverride string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.Error("Starting timer", "duration", int(d.Seconds()))
|
logger.Error("Starting timer", "duration", int(d.Seconds()))
|
||||||
err := haClient.CallService(t.getEntityId(), homeassistant.Services.Start, map[string]any{
|
err := haClient.CallService(t.Name, homeassistant.Services.Start, map[string]any{
|
||||||
homeassistant.ExtraProps.Duration: int(d.Seconds()),
|
homeassistant.ExtraProps.Duration: int(d.Seconds()),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -75,18 +75,17 @@ func (t *HatsTimer) Activate(durationOverride string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HatsTimer) ActivateIfNotAlready(durationOverride string) {
|
func (t *HatsTimer) ActivateIfNotAlready(durationOverride string) {
|
||||||
state, err := haClient.GetState(t.getEntityId())
|
if state, err := haClient.GetState(t.Name); err == nil && !homeassistant.StateToBool(state.State) {
|
||||||
if err != nil || state.State != "active" {
|
|
||||||
t.Activate(durationOverride)
|
t.Activate(durationOverride)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HatsTimer) Cancel() {
|
func (t *HatsTimer) Cancel() {
|
||||||
haClient.CallService(t.getEntityId(), homeassistant.Services.Cancel)
|
haClient.CallService(t.Name, homeassistant.Services.Cancel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *HatsTimer) ToString() string {
|
func (t *HatsTimer) ToString() string {
|
||||||
if t.State == "active" {
|
if homeassistant.StateToBool(t.State) {
|
||||||
time, _ := t.FinishTime.MarshalText()
|
time, _ := t.FinishTime.MarshalText()
|
||||||
return string(time)
|
return string(time)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
// States that return true: "on", "home", "open", "playing", non-zero numbers, etc.
|
// States that return true: "on", "home", "open", "playing", non-zero numbers, etc.
|
||||||
// All others return false
|
// All others return false
|
||||||
func StateToBool(state string) bool {
|
func StateToBool(state string) bool {
|
||||||
trueRegex := regexp.MustCompile(`^(on|home|open(ing)?|unlocked|playing|good|walking|charging|alive|heat|cool|heat_cool|[1-9][\d\.]*|0\.0*[1-9]\d*)$`)
|
trueRegex := regexp.MustCompile(`^(on|home|open(ing)?|unlocked|playing|active|good|walking|charging|alive|heat|cool|heat_cool|[1-9][\d\.]*|0\.0*[1-9]\d*)$`)
|
||||||
return trueRegex.MatchString(state)
|
return trueRegex.MatchString(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue