From 75f36d95c7690cc9db4f04c7aa3c8749c061bbe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Grill?= Date: Thu, 4 Jul 2024 06:45:49 +0200 Subject: [PATCH] position signal to shutter --- config.go | 11 ++++++----- devices.go | 2 ++ proxy/shutter.go | 30 ++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index 4e6aa0f..6a50bc8 100644 --- a/config.go +++ b/config.go @@ -16,11 +16,12 @@ type BasicConfig struct { type Cover struct { BasicConfig - CommandTopic string `json:"command_topic,omitempty"` - Name string `json:"name,omitempty"` - PositionTopic string `json:"position_topic,omitempty"` - PositionOpen int `json:"position_open"` - PositionClosed int `json:"position_closed"` + CommandTopic string `json:"command_topic,omitempty"` + Name string `json:"name,omitempty"` + PositionTopic string `json:"position_topic,omitempty"` + PositionTemplate string `json:"position_template,omitempty"` + PositionOpen int `json:"position_open"` + PositionClosed int `json:"position_closed"` } type DInput struct { diff --git a/devices.go b/devices.go index b33b36e..3f505fc 100644 --- a/devices.go +++ b/devices.go @@ -37,6 +37,8 @@ type IBasicShutter interface { Open() Close() Stop() + Range() int + Position() ISensor[int] } type IShutter interface { diff --git a/proxy/shutter.go b/proxy/shutter.go index a3477a1..2efcc28 100644 --- a/proxy/shutter.go +++ b/proxy/shutter.go @@ -1,10 +1,26 @@ package proxy -import "github.com/home2mqtt/hass" +import ( + "strconv" + + "github.com/home2mqtt/hass" +) type shutter struct { baseActuator - basic bool + basic bool + positionrange int + position hass.ISensor[int] +} + +// Position implements hass.IShutter. +func (s *shutter) Position() hass.ISensor[int] { + return s.position +} + +// Range implements hass.IShutter. +func (s *shutter) Range() int { + return s.positionrange } func BasicShutter(runtime hass.IPubSubRuntime, conf *hass.Cover) hass.IShutter { @@ -17,6 +33,16 @@ func BasicShutter(runtime hass.IPubSubRuntime, conf *hass.Cover) hass.IShutter { func Shutter(runtime hass.IPubSubRuntime, conf *hass.Cover) hass.IShutter { result := &shutter{} + if conf.PositionOpen != 0 { + result.positionrange = conf.PositionOpen - conf.PositionClosed + } else { + result.positionrange = 100 + } + if conf.PositionTopic != "" { + result.position = hass.ChanToSensor(ParseSensorValue(runtime, conf.PositionTopic, conf.PositionTemplate, func(s string) (int, error) { + return strconv.Atoi(s) + })) + } result.init(runtime, conf.CommandTopic) return result }