Skip to content

Commit

Permalink
restructured to distinguish "bridge" and "proxy" code
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsgrill committed Jun 21, 2024
1 parent 9c65399 commit 1db512f
Show file tree
Hide file tree
Showing 15 changed files with 559 additions and 305 deletions.
93 changes: 5 additions & 88 deletions base.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package hass

import (
"fmt"
"strconv"
"strings"
)

var getStatePayload = []byte("{\"state\":\"\"}")

type BaseSensor[SensorEvent any] struct {
events chan SensorEvent
}
Expand All @@ -21,89 +13,14 @@ func (s *BaseSensor[SensorEvent]) Events() chan SensorEvent {
return s.events
}

func NewSensor[SensorEvent any]() ISensor[SensorEvent] {
func ChanToSensor[SensorEvent any](c chan SensorEvent) ISensor[SensorEvent] {
return &BaseSensor[SensorEvent]{
events: make(chan SensorEvent),
}
}

type baseActuator struct {
client IPubSubRuntime
topic string
}

func (b *baseActuator) init(context IPubSubRuntime, topic string) {
b.topic = topic
b.client = context
}

func (s *baseActuator) send(action string) {
s.client.Send(s.topic, []byte(action))
}

type stateField[Type any] struct {
actuator *baseActuator
values chan Type
key string
}

func (state *stateField[Type]) send(command string) {
state.actuator.send(fmt.Sprintf("{\"%s\":\"%s\"}", state.key, command))
}

func (d *stateField[_]) Close() error {
close(d.values)
return nil
}

func (d *stateField[T]) Events() chan T {
return d.values
}

type intStateField struct {
stateField[int]
scale int
}

func (d *intStateField) SetValue(value int) {
d.send(strconv.Itoa(value))
}

func (field *intStateField) Process(state map[string]interface{}) {
if value, exists := state[field.key]; exists {
v, err := strconv.Atoi(fmt.Sprint(value))
if err != nil {
field.values <- v
}
}
}

func (field *intStateField) Scale() int {
return field.scale
}

type onOffStateField struct {
stateField[bool]
}

func (field *onOffStateField) Process(state map[string]interface{}) {
if value, exists := state[field.key]; exists {
str := fmt.Sprint(value)
if strings.EqualFold("ON", str) {
field.values <- true
}
if strings.EqualFold("OFF", str) {
field.values <- false
}
events: c,
}
}

func (d *onOffStateField) SetValue(on bool) {
var v string
if on {
v = "ON"
} else {
v = "OFF"
func NewSensor[SensorEvent any]() ISensor[SensorEvent] {
return &BaseSensor[SensorEvent]{
events: make(chan SensorEvent),
}
d.send(v)
}
6 changes: 4 additions & 2 deletions announce.go → bridge/announce.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package hass
package bridge

import (
"encoding/json"

"github.com/home2mqtt/hass"
)

// <discovery_prefix>/<component>/[<node_id>/]<object_id>/config
func AnnounceDevice(client IPubSubRuntime, prefix string, nodeid string, objectid string, device IConfig) error {
func AnnounceDevice(client hass.IPubSubRuntime, prefix string, nodeid string, objectid string, device hass.IConfig) error {
c, err := json.Marshal(device)
if err != nil {
return err
Expand Down
22 changes: 22 additions & 0 deletions bridge/fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package bridge

import (
"fmt"

"github.com/home2mqtt/hass"
)

func AttachSensor[T any](runtime hass.IPubSubRuntime, stateTopic string, valueTemplate string, sensor hass.ISensor[T]) {
go func() {
for v := range sensor.Events() {
runtime.Send(stateTopic, []byte(fmt.Sprint(v)))
}
}()
}

func AttachField[T any](runtime hass.IPubSubRuntime, stateTopic string, commandTopic string, field hass.IField[T]) {
runtime.Receive(commandTopic, func(topic string, payload []byte) {
field.SetValue(field.ParseValue(payload))

Check failure on line 19 in bridge/fields.go

View workflow job for this annotation

GitHub Actions / build

field.ParseValue undefined (type hass.IField[T] has no field or method ParseValue)
})
AttachSensor[T](runtime, stateTopic, "", field)
}
30 changes: 23 additions & 7 deletions devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,32 @@ type ISensor[SensorEvent any] interface {
Events() chan SensorEvent
}

type IField[Type any] interface {
ISensor[Type]
SetValue(value Type)
}

type IBoolField interface {
IField[bool]
Toggle()
}

type IEnumField interface {
IField[string]
List() []string
}

type ActionEvent struct {
Action string
}

type IHVAC[StateType any] interface {
Stop() (StateType, error)
Restart(StateType) error
State() StateType
IsOn(StateType) bool
type IHVAC interface {
Power() IField[bool]
Mode() IEnumField
Fan() IEnumField
Swing() IEnumField
TargetTemp() IField[float64]
Temp() ISensor[float64]
}

type IBasicShutter interface {
Expand Down Expand Up @@ -41,7 +58,6 @@ type ILight interface {
}

type IIntSettable interface {
ISensor[int]
SetValue(value int)
IField[int]
Scale() int
}
94 changes: 0 additions & 94 deletions light.go

This file was deleted.

17 changes: 17 additions & 0 deletions proxy/baseactuator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package proxy

import "github.com/home2mqtt/hass"

type baseActuator struct {
client hass.IPubSubRuntime
topic string
}

func (b *baseActuator) init(context hass.IPubSubRuntime, topic string) {
b.topic = topic
b.client = context
}

func (s *baseActuator) send(action string) {
s.client.Send(s.topic, []byte(action))
}
Loading

0 comments on commit 1db512f

Please sign in to comment.