Skip to content

Commit

Permalink
make sensor work with rules
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilGruber committed Dec 2, 2024
1 parent f145bd3 commit 627e279
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
1 change: 0 additions & 1 deletion devices/dimmable.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func (d *Dimmable) setTarget(target float64) {
}

func (d *Dimmable) ProcessRequest(request core.SwitchRequest) {
log.Printf("Setting %s to %s within %d seconds\n", d.GetMqttTopic(), request.Value, request.Duration)
value, _ := strconv.ParseFloat(request.Value, 64)
value = math.Min(value, 100)
value = math.Max(value, 0)
Expand Down
10 changes: 9 additions & 1 deletion devices/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (r *Rule) Fire(channel chan core.SwitchRequest) []Receiver {
}

func (r *Rule) checkCondition(value any, condition string, target any) bool {
// log.Printf("Checking condition %v %s %v\n", value, condition, target)
switch condition {
case "==":
return value == target
Expand All @@ -106,7 +107,14 @@ func (r *Rule) checkCondition(value any, condition string, target any) bool {
case ">":
switch target.(type) {
case int:
return value.(int) > target.(int)
var val int
switch value.(type) {
case int:
val = value.(int)
case int64:
val = int(value.(int64))
}
return val > target.(int)
case float64:
return value.(float64) > target.(float64)
}
Expand Down
32 changes: 16 additions & 16 deletions devices/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ type Sensor struct {
func MakeSensor(config core.DeviceConfig) Sensor {
s := Sensor{}
s.setBaseConfig(config)
s.MqttState = config.Topic

s.Max = 100
s.Min = 0

s.Active = false
s.Current = 0
s.Target = 0

s.Type = "sensor"
return s
}
Expand All @@ -46,19 +46,11 @@ type SensorMessageWrapper struct {
func (s *Sensor) PublishValue(mqtt.Client) {
}

func (s *Sensor) GetStateMqttTopic() string {
return s.MqttState
}

func (s *Sensor) GetMessageHandler(channel chan core.SwitchRequest, _ DeviceInterface) mqtt.MessageHandler {
func (s *Sensor) GetMessageHandler(_ chan core.SwitchRequest, _ DeviceInterface) mqtt.MessageHandler {
return func(client mqtt.Client, mqttMessage mqtt.Message) {

payload := mqttMessage.Payload()

if s.GetCurrent() == 0 {
return
}

var data SensorMessageWrapper
err := json.Unmarshal(payload, &data)
if err != nil {
Expand All @@ -69,19 +61,27 @@ func (s *Sensor) GetMessageHandler(channel chan core.SwitchRequest, _ DeviceInte
message := data.TuyaReceived

if message.Cmnd == 5 || message.Cmnd == 2 {
s.Active = true
now := time.Now()
s.LastChanged = &now
log.Printf("Motion detected (%d)", message.Cmnd)
request, ok := s.GenerateRequest(message.CmndData)
if ok {
channel <- request
}
}

}
}

func (s *Sensor) GetTriggerValue(key string) interface{} {
if key == "noMotion" {
return time.Now().Unix() - s.LastChanged.Unix()
if s.LastChanged != nil {
return time.Now().Unix() - s.LastChanged.Unix()
}
return 0
}
if key == "motion" {
if s.Active {
s.Active = false
return 1
}
}
return nil
}

0 comments on commit 627e279

Please sign in to comment.