tibber

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2020 License: MIT Imports: 9 Imported by: 3 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

tibber-golang

Limited implementation of the Tibber API in golang
developer.tibber.com

Possibilities:

  • Get a list of homes with id, name, meterID and features
  • Send push notification from the Tibber App
  • Subscribe to data from Tibber Pulse

Usage

package main

import (
	"fmt"
	"strconv"

	tibber "github.com/tskaard/tibber-golang"
)
const token = "<Tibber token>"

type Handler struct {
	tibber     *tibber.Client
	streams    map[string]*tibber.Stream
	msgChannal tibber.MsgChan
}

func NewHandler() *Handler {
	h := &Handler{}
	h.tibber = tibber.NewClient("")
	h.streams = make(map[string]*tibber.Stream)
	h.msgChannal = make(tibber.MsgChan)
	return h
}

func main() {
    h := NewHandler()
	h.tibber.Token = token
	homes, err := h.tibber.GetHomes()
	if err != nil {
		panic("Can not get homes from Tibber")
	}
	for _, home := range homes {
		fmt.Println(home.ID)
		if home.Features.RealTimeConsumptionEnabled {
			stream := tibber.NewStream(home.ID, h.tibber.Token)
			stream.StartSubscription(h.msgChannal)
			h.streams[home.ID] = stream
		}
	}
	_, err = h.tibber.SendPushNotification("Tibber-Golang", "Message from GO")
	if err != nil {
		panic("Push failed")
	}
	go func(msgChan tibber.MsgChan) {
		for {
			select {
			case msg := <-msgChan:
				h.handleStreams(msg)
			}
		}
	}(h.msgChannal)

	for {
	}

}

func (h *Handler) handleStreams(newMsg *tibber.StreamMsg) {
	fmt.Println(newMsg.Payload.Data.LiveMeasurement.Timestamp + " :: " + strconv.Itoa(newMsg.Payload.Data.LiveMeasurement.Power) + " Watt")
}

Documentation

Index

Constants

View Source
const (
	StreamStateConnected    = "CONNECTED"
	StreamStateConnecting   = "CONNECTING"
	StreamStateDisconnected = "DISCONNECTED"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Address added in v0.0.4

type Address struct {
	Address1   string `json:"address1"`
	Address2   string `json:"address2"`
	Address3   string `json:"address3"`
	PostalCode string `json:"postalCode"`
	City       string `json:"city"`
	Country    string `json:"country"`
	Latitude   string `json:"latitude"`
	Longitude  string `json:"longitude"`
}

type Client

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

Client for requests and streams

func NewClient

func NewClient(token string) *Client

NewClient init tibber client

func (*Client) GetCurrentPrice added in v0.0.5

func (t *Client) GetCurrentPrice(homeId string) (CurrentPriceInfo, error)

func (*Client) GetHomeById added in v0.0.4

func (t *Client) GetHomeById(homeId string) (Home, error)

GetHomeById get a home with information

func (*Client) GetHomes

func (t *Client) GetHomes() ([]Home, error)

GetHomes get a list of homes with information

func (*Client) SendPushNotification

func (t *Client) SendPushNotification(title, msg string) (int, error)

SendPushNotification from tibber app

type CurrentPriceInfo added in v0.0.5

type CurrentPriceInfo struct {
	Level    string    `json:"level"`
	Total    float64   `json:"total"`
	Energy   float64   `json:"energy"`
	Tax      float64   `json:"tax"`
	Currency string    `json:"currency"`
	StartsAt time.Time `json:"startsAt"`
}

type CurrentSubscription added in v0.0.5

type CurrentSubscription struct {
	PriceInfo PriceInfo `json:"priceInfo"`
}

type Data

type Data struct {
	LiveMeasurement LiveMeasurement `json:"liveMeasurement"`
}

Data in Payload

type Features

type Features struct {
	RealTimeConsumptionEnabled bool `json:"realTimeConsumptionEnabled"`
}

Features - tibber pulse connected

type Home

type Home struct {
	ID                   string              `json:"id"`
	AppNickname          string              `json:"appNickname"`
	MeteringPointData    MeteringPointData   `json:"meteringPointData"`
	Features             Features            `json:"features"`
	Address              Address             `json:"address"`
	Size                 int                 `json:"size"`
	MainFuseSize         int                 `json:"mainFuseSize"`
	NumberOfResidents    int                 `json:"numberOfResidents"`
	PrimaryHeatingSource string              `json:"primaryHeatingSource"`
	HasVentilationSystem bool                `json:"hasVentilationSystem"`
	CurrentSubscription  CurrentSubscription `json:"currentSubscription"`
	PreviousMeterData    PreviousMeterData   `json:"previousMeterData"`
}

Home structure

type HomeResponse added in v0.0.4

type HomeResponse struct {
	Viewer HomeViewer `json:"viewer"`
}

type HomeViewer

type HomeViewer struct {
	Home Home `json:"home"`
}

type HomesResponse

type HomesResponse struct {
	Viewer HomesViewer `json:"viewer"`
}

HomesResponse response from homes

type HomesViewer added in v0.0.4

type HomesViewer struct {
	Homes []Home `json:"homes"`
}

HomeViewer list of homes

type LiveMeasurement

type LiveMeasurement struct {
	Timestamp              time.Time `json:"timestamp"`
	Power                  float64   `json:"power"`
	LastMeterConsumption   float64   `json:"lastMeterConsumption"`
	LastMeterProduction    float64   `json:"lastMeterProduction"`
	AccumulatedConsumption float64   `json:"accumulatedConsumption"`
	AccumulatedCost        float64   `json:"accumulatedCost"`
	AccumulatedProduction  float64   `json:"accumulatedProduction"`
	AccumulatedReward      float64   `json:"accumulatedReward"`
	MinPower               float64   `json:"minPower"`
	AveragePower           float64   `json:"averagePower"`
	MaxPower               float64   `json:"maxPower"`
	PowerProduction        float64   `json:"powerProduction"`
	MinPowerProduction     float64   `json:"minPowerProduction"`
	MaxPowerProduction     float64   `json:"maxPowerProduction"`
	VoltagePhase1          float64   `json:"voltagePhase1"`
	VoltagePhase2          float64   `json:"voltagePhase2"`
	VoltagePhase3          float64   `json:"voltagePhase3"`
	CurrentPhase1          float64   `json:"currentPhase1"`
	CurrentPhase2          float64   `json:"currentPhase2"`
	CurrentPhase3          float64   `json:"currentPhase3"`
}

LiveMeasurement in data payload

func (*LiveMeasurement) AsFloatMap added in v0.1.1

func (m *LiveMeasurement) AsFloatMap() map[string]float64

AsFloatMap returns the LiveMeasurement struct as a float map

func (*LiveMeasurement) HasPower added in v0.2.0

func (m *LiveMeasurement) HasPower() bool

HasPower returns true if the report contains power measurement

func (*LiveMeasurement) HasProductionOrConsumptionPower added in v0.2.1

func (m *LiveMeasurement) HasProductionOrConsumptionPower() bool

HasProductionOrConsumptionPower return true if measurement contains values

func (*LiveMeasurement) IsExtended added in v0.1.1

func (m *LiveMeasurement) IsExtended() bool

IsExtended returns whether the report is normal or extended. In an extended report we would have at least one phase information

type MeteringPointData

type MeteringPointData struct {
	ConsumptionEan string `json:"consumptionEan"`
}

MeteringPointData - meter number

type MsgChan

type MsgChan chan *StreamMsg

MsgChan for reciving messages

type Payload

type Payload struct {
	Data Data `json:"data"`
}

Payload in StreamMsg

type PreviousMeterData added in v0.2.0

type PreviousMeterData struct {
	Power           float64 `json:"power"`
	PowerProduction float64 `json:"powerProduction"`
}

type PriceInfo added in v0.0.5

type PriceInfo struct {
	CurrentPriceInfo CurrentPriceInfo `json:"current"`
}

type PushInput

type PushInput struct {
	Title        string `json:"title"`
	Message      string `json:"message"`
	ScreenToOpen string `json:"screenToOpen"`
}

PushInput push message

type PushResponse

type PushResponse struct {
	SendPushNotification SendPushNotification `json:"sendPushNotification"`
}

PushResponse respons from notification api

type SendPushNotification

type SendPushNotification struct {
	Successful              bool `json:"successful"`
	PushedToNumberOfDevices int  `json:"pushedToNumberOfDevices"`
}

SendPushNotification data in push response

type Stream

type Stream struct {
	Token string
	ID    string
	// contains filtered or unexported fields
}

Stream for subscribing to Tibber pulse

func NewStream

func NewStream(id, token string) *Stream

NewStream with id and token

func (*Stream) StartSubscription

func (ts *Stream) StartSubscription(outputChan MsgChan) error

StartSubscription init connection and subscribes to home id

func (*Stream) StateReportChan added in v0.2.0

func (ts *Stream) StateReportChan() chan StreamState

func (*Stream) Stop

func (ts *Stream) Stop()

Stop stops stream

type StreamMsg

type StreamMsg struct {
	HomeID  string  `json:"homeId"`
	Type    string  `json:"type"`
	ID      int     `json:"id"`
	Payload Payload `json:"payload"`
}

StreamMsg for streams

type StreamState added in v0.2.0

type StreamState struct {
	State string
	Err   error
}

Jump to

Keyboard shortcuts

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