From 35c7a5c0e5083e8d795f6c68a73bce9890793bf3 Mon Sep 17 00:00:00 2001 From: Jordan Hotmann Date: Mon, 16 Oct 2023 15:20:08 -0600 Subject: [PATCH] Get state with default --- pkg/client/client.go | 8 ++++++++ pkg/homeassistant/structs.go | 34 ++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index d8d1974..1582900 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -46,6 +46,14 @@ func (c *HatsClient) GetState(entityId string) (string, error) { return resp.String(), nil } +func (c *HatsClient) GetStateWithDefault(entityId string, defaultValue string) string { + state, err := c.GetState(entityId) + if err != nil { + return defaultValue + } + return state +} + func (c *HatsClient) GetStateBool(entityId string) (bool, error) { stateString, err := c.GetState(entityId) diff --git a/pkg/homeassistant/structs.go b/pkg/homeassistant/structs.go index 0a3c817..6b2def8 100644 --- a/pkg/homeassistant/structs.go +++ b/pkg/homeassistant/structs.go @@ -38,23 +38,25 @@ var Domains = struct { // Home Assistant services var Services = struct { - TurnOn string - TurnOff string - Toggle string - Reload string - Lock string - Unlock string - OpenCover string - CloseCover string + TurnOn string + TurnOff string + Toggle string + Reload string + Lock string + Unlock string + OpenCover string + CloseCover string + SelectOption string }{ - TurnOn: "turn_on", - TurnOff: "turn_off", - Toggle: "toggle", - Reload: "reload", - Lock: "lock", - Unlock: "unlock", - OpenCover: "open_cover", - CloseCover: "close_cover", + TurnOn: "turn_on", + TurnOff: "turn_off", + Toggle: "toggle", + Reload: "reload", + Lock: "lock", + Unlock: "unlock", + OpenCover: "open_cover", + CloseCover: "close_cover", + SelectOption: "select_option", } // Extra props that can be sent when calling a Home Assistant service