Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Tasmota outputs #99

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions tasmota/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/reef-pi/hal"
"io"
"io/ioutil"
"net/http"
"sync"
"time"
Expand All @@ -15,6 +14,7 @@ import (
type httpDriver struct {
meta hal.Metadata
address string
output int
}

func (m *httpDriver) Close() error {
Expand Down Expand Up @@ -65,16 +65,16 @@ func (m *httpDriver) doRequest(url string) (*http.Response, error) {

func (m *httpDriver) readBody(body io.ReadCloser) ([]byte, error) {
defer body.Close()
msg, err := ioutil.ReadAll(body)
msg, err := io.ReadAll(body)
if err != nil {
return nil, err
}
return msg, nil
}

func (m *httpDriver) LastState() bool {
const urlBase = "http://%s/cm?cmnd=Power0"
uri := fmt.Sprintf(urlBase, m.address)
const urlBase = "http://%s/cm?cmnd=Power%d"
uri := fmt.Sprintf(urlBase, m.address, m.output)
resp, err := m.doRequest(uri)
if err != nil {
return false
Expand All @@ -91,9 +91,16 @@ func (m *httpDriver) LastState() bool {
if err != nil {
return false
}
const power = "POWER"
const on = "ON"
return result[power] == on

if result[fmt.Sprintf("POWER%d", m.output)] == "ON" {
return true
}

if result["POWER"] == "ON" {
return true
}

return false
}

func (m *httpDriver) Set(value float64) error {
Expand All @@ -114,8 +121,8 @@ func (m *httpDriver) Set(value float64) error {
}

func (m *httpDriver) Write(b bool) error {
const baseUri = "http://%s/cm?cmnd=Power0%%20%t"
uri := fmt.Sprintf(baseUri, m.address, b)
const baseUri = "http://%s/cm?cmnd=Power%d%%20%t"
uri := fmt.Sprintf(baseUri, m.address, m.output, b)
resp, err := m.doRequest(uri)
if err != nil {
return err
Expand Down Expand Up @@ -147,6 +154,7 @@ var pwmDriverFactory *factory
var once sync.Once

const address = "Address"
const output = "Output"

func HttpDriverFactory() hal.DriverFactory {

Expand All @@ -164,6 +172,12 @@ func HttpDriverFactory() hal.DriverFactory {
Order: 0,
Default: "192.1.168.4",
},
{
Name: output,
Type: hal.Integer,
Order: 1,
Default: 0,
},
},
}
})
Expand Down Expand Up @@ -195,6 +209,21 @@ func (f *factory) ValidateParameters(parameters map[string]interface{}) (bool, m
failures[address] = append(failures[address], failure)
}

if v, ok := parameters[output]; ok {
val, ok := v.(int)
if !ok {
failure := fmt.Sprint(output, " is not an integer. ", v, " was received.")
failures[output] = append(failures[output], failure)

} else if val < 0 {
failure := fmt.Sprint(output, " value should be greater than 0. ", val, " was received.")
failures[output] = append(failures[output], failure)
}
} else {
failure := fmt.Sprint(output, " is a required parameter, but was not received.")
failures[output] = append(failures[output], failure)
}

return len(failures) == 0, failures
}

Expand All @@ -209,6 +238,7 @@ func (f *factory) NewDriver(parameters map[string]interface{}, hardwareResources
driver := &httpDriver{
meta: f.meta,
address: parameters[address].(string),
output: parameters[output].(int),
}
return driver, nil
}
7 changes: 5 additions & 2 deletions tasmota/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ func TestHttpDriver_AsDigitalOut(t *testing.T) {

address := os.Getenv("TASMOTA_TEST_ADDRESS")

if len(address) == 0 {
if len(address) == 0 {
address = "192.168.1.46"
}

f := HttpDriverFactory()

params := map[string]interface{}{
"Address": address,
"Output": 2,
}

d, err := f.NewDriver(params, nil)
Expand Down Expand Up @@ -81,14 +82,15 @@ func TestHttpDriver_AsPWMDriver(t *testing.T) {

address := os.Getenv("TASMOTA_TEST_ADDRESS")

if len(address) == 0 {
if len(address) == 0 {
address = "192.168.1.46"
}

f := HttpDriverFactory()

params := map[string]interface{}{
"Address": address,
"Output": 0,
}

d, err := f.NewDriver(params, nil)
Expand Down Expand Up @@ -154,6 +156,7 @@ func TestHttpDriver_FactoryValidateParameters(t *testing.T) {

params := map[string]interface{}{
"Address": "192.168.1.46",
"Output": 0,
}

_, err := f.NewDriver(params, nil)
Expand Down
Loading