From 9edf4ee12230d910cd9f18dbcb2af58457af93ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Grill?= Date: Sat, 29 Jun 2024 14:56:06 +0200 Subject: [PATCH] bridge base structures --- bridge/fields.go | 10 ++++-- bridge/properties.go | 84 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 bridge/properties.go diff --git a/bridge/fields.go b/bridge/fields.go index 1407679..113d57e 100644 --- a/bridge/fields.go +++ b/bridge/fields.go @@ -2,6 +2,7 @@ package bridge import ( "fmt" + "log" "github.com/home2mqtt/hass" ) @@ -14,9 +15,14 @@ func AttachSensor[T any](runtime hass.IPubSubRuntime, stateTopic string, valueTe }() } -func AttachField[T any](runtime hass.IPubSubRuntime, stateTopic string, commandTopic string, field hass.IField[T]) { +func AttachField[T any](runtime hass.IPubSubRuntime, stateTopic string, commandTopic string, field hass.IField[T], parseValue func(str string) (T, error)) { runtime.Receive(commandTopic, func(topic string, payload []byte) { - field.SetValue(field.ParseValue(payload)) + value, err := parseValue(string(payload)) + if err != nil { + log.Println(err) + return + } + field.SetValue(value) }) AttachSensor[T](runtime, stateTopic, "", field) } diff --git a/bridge/properties.go b/bridge/properties.go new file mode 100644 index 0000000..cf1d244 --- /dev/null +++ b/bridge/properties.go @@ -0,0 +1,84 @@ +package bridge + +import ( + "log" + "strings" + + "github.com/home2mqtt/hass" +) + +type PropertyContext struct { + hass.IPubSubRuntime + Base string + Id string +} + +type IProperty[T any] interface { + StateTopic() string + CommandTopic() string + NotifyState(value T) + OnCommand(callback func(value T)) +} + +func (pc *PropertyContext) DefineString(name string) IProperty[string] { + return &stringProperty{ + property: property{ + PropertyContext: pc, + name: name, + }, + } +} + +func (pc *PropertyContext) DefineFloat(name string) IProperty[float64] { + return &floatProperty{ + property: property{ + PropertyContext: pc, + name: name, + }, + } +} + +type property struct { + *PropertyContext + name string +} + +func (p *property) StateTopic() string { + return strings.Join([]string{p.Base, p.Id, p.name}, "/") +} + +func (p *property) CommandTopic() string { + return strings.Join([]string{p.Base, p.Id, p.name, "set"}, "/") +} + +type stringProperty struct { + property +} + +func (p *stringProperty) NotifyState(value string) { + hass.SendString(p, p.StateTopic(), value) +} + +func (p *stringProperty) OnCommand(callback func(value string)) { + hass.ReceiveString(p, p.CommandTopic(), func(topic, payload string) { + callback(payload) + }) +} + +type floatProperty struct { + property +} + +func (p *floatProperty) NotifyState(value float64) { + hass.SendFloat(p, p.StateTopic(), value) +} + +func (p *floatProperty) OnCommand(callback func(value float64)) { + hass.ReceiveFloat(p, p.CommandTopic(), func(topic string, payload float64, err error) { + if err == nil { + callback(payload) + } else { + log.Printf("Float value error received on %s: %v\n", topic, err) + } + }) +}