mystrom

package module
v0.0.0-...-eaada3e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 11 Imported by: 0

README

mystrom

Go Reference Go Report Card

The mystrom package provides Go client for the myStrom REST API.

Features

  • Switch
  • Button
  • Bulb
  • LED Strip
  • PIR
  • New Button Plus
  • Discovery

PR's for additional endpoints are welcome!

Usage

To install the mystrom package, use the following command:

package main

import (
	"context"
	"log"
	"net"
	"net/url"

	"thde.io/mystrom"
)

func main() {
	client := mystrom.NewClient()

	for {
		discover := mystrom.Discover{}
		device, err := discover.Device(context.Background())
		if err != nil {
			log.Fatal(err)
		}

		log.Printf("%+v", device)

		host, _, err := net.SplitHostPort(device.Address.String())
		if err != nil {
			log.Fatal(err)
		}
		if device.Type != mystrom.DeviceTypeSwitchCH && device.Type != mystrom.DeviceTypeSwitchEU {
			continue
		}

		u, err := url.Parse("http://" + host)
		if err != nil {
			log.Fatal(err)
		}
		sw := client.NewSwitch(u)
		temp, err := sw.Temperature(context.Background())
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("%+v", temp)
	}
}

CLI

To install the CLI, run:

go install thde.io/mystrom/cmd/mystrom@latest

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrStatus = errors.New("status code error")

ErrStatus respresents a non success status code error.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(opts ...Option) *Client

func (*Client) NewSwitch

func (c *Client) NewSwitch(baseURL *url.URL) *Switch

NewSwitch creates a new Switch instance.

type Device

type Device struct {
	Address net.Addr
	MAC     net.HardwareAddr
	Type    DeviceType

	Cloud      bool
	Registered bool
	MeshChild  bool
}

type DeviceType

type DeviceType byte
const (
	DeviceTypeBulb                    DeviceType = 102
	DeviceTypeButtonPlus1stGeneration DeviceType = 103
	DeviceTypeButtonSmall             DeviceType = 104
	DeviceTypeLEDStrip                DeviceType = 105
	DeviceTypeSwitchCH                DeviceType = 106
	DeviceTypeSwitchEU                DeviceType = 107
	DeviceTypeMotionSensor            DeviceType = 110
	DeviceTypeGateway                 DeviceType = 112
	DeviceTypeSTECCO                  DeviceType = 113
	DeviceTypeButtonPlus2ndGeneration DeviceType = 118
	DeviceTypeSwitchZero              DeviceType = 120
)

https://api.mystrom.ch -> Discovery devices -> Device types

func (DeviceType) String

func (d DeviceType) String() string

type Discover

type Discover struct {
	// See func net.Dial for a description of the Address parameter.
	Address string
	// Network must be "udp", "udp4", "udp6", "unixgram", or an IP transport.
	// See net.ListenPacket for more details on the Network parameter.
	Network      string
	ListenConfig net.ListenConfig
}

func (*Discover) Device

func (d *Discover) Device(ctx context.Context) (Device, error)

Device blocks until a MyStrom device has been discovered. Each device cyclically (every 5 seconds) sends a broadcast packet using the UDP protocol to the address 255.255.255.255 and port 7979.

type Option

type Option func(*Client)

func WithAPIKey

func WithAPIKey(key string) Option

WithAPIKey defines the API key to be used.

func WithHTTPClient

func WithHTTPClient(h *http.Client) Option

WithHTTPClient allows to replace the http client.

func WithUserAgent

func WithUserAgent(userAgent string) Option

WithUserAgent allows to change the user agent.

type RelaySwitchState

type RelaySwitchState string
const (
	RelaySwitchStateOn  RelaySwitchState = "1"
	RelaySwitchStateOff RelaySwitchState = "0"
)

type Switch

type Switch struct {
	// contains filtered or unexported fields
}

Switch holds all info and logic to talk your myStrom Switch device.

func NewSwitch

func NewSwitch(baseURL *url.URL) *Switch

NewSwitch creates a new Switch instance with a default client.

func (Switch) Off

func (s Switch) Off(ctx context.Context) error

Off turns the power of the Switch off.

func (Switch) On

func (s Switch) On(ctx context.Context) error

On turns the power of the Switch on.

func (Switch) PowerCycle

func (s Switch) PowerCycle(ctx context.Context, wait time.Duration) error

PowerCycle turns the switch off, waits for a specified amount of time (max 1h), then starts it again. The switch has to be turned on in order for this call to work.

func (Switch) Relay

func (s Switch) Relay(ctx context.Context, state RelaySwitchState) error

func (Switch) Report

func (s Switch) Report(ctx context.Context) (*SwitchReport, error)

Report returns a report of the current statut of the Switch.

func (Switch) Temperature

func (s Switch) Temperature(ctx context.Context) (*SwitchTemperature, error)

Temperature returns the current temperature in °C.

func (Switch) Timer

func (s Switch) Timer(ctx context.Context, mode SwitchTimerMode, time time.Duration) error

Timer sets the relay state for a given time, after the time has elapsed the state of the relay is reversed.

func (Switch) Toggle

func (s Switch) Toggle(ctx context.Context) error

Toggle toggles the power state of the Switch.

func (Switch) URL

func (s Switch) URL() url.URL

type SwitchReport

type SwitchReport struct {
	Power float64 `json:"power"` // current power consumption in watts
	Relay bool    `json:"relay"` // state of the Switch, true is on, false is off
}

SwitchReport represets the content of a report of the Switch.

type SwitchTemperature

type SwitchTemperature struct {
	Measured     float64 `json:"measured"`     // measured temp
	Compensation float64 `json:"compensation"` // assumed gap
	Compensated  float64 `json:"compensated"`  // real temp
}

SwitchTemperature represets the content of a temperature response of the Switch. All temperatures are provided in °C.

type SwitchTimerMode

type SwitchTimerMode string
const (
	SwitchTimerModeOn     SwitchTimerMode = "on"
	SwitchTimerModeOff    SwitchTimerMode = "off"
	SwitchTimerModeToggle SwitchTimerMode = "toggle"
	SwitchTimerModeNone   SwitchTimerMode = "none"
)

Directories

Path Synopsis
cmd
mystrom command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL