exampleapp

package
v10.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownRequest     = errors.New("did not understand request")
	ErrFetchWeatherFailed = errors.New("could not get weather data")
	ErrFetchLoadFailed    = errors.New("could not get load averages")
)

Functions

This section is empty.

Types

type ExampleApp

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

func New

func New(_ context.Context) (*ExampleApp, error)

New sets up our example app. We make use of the preference loading/saving in the agent to handle our preferences.

func (*ExampleApp) Configuration

func (a *ExampleApp) Configuration() []*mqttapi.Msg

Configuration is called when our app is first registered in Home Assistant and will return configuration messages for the data our app will send/receive.

func (*ExampleApp) MsgCh

func (a *ExampleApp) MsgCh() chan *mqttapi.Msg

MsgCh returns a channel through which we could pass any message to MQTT on any kind of custom event trigger or other non time-based polling.

func (*ExampleApp) Name

func (a *ExampleApp) Name() string

Name simply returns the name of this app.

func (*ExampleApp) PollConfig

func (a *ExampleApp) PollConfig() (interval, jitter time.Duration)

PollConfig defines our polling interval and jitter and instructs the agent to fetch our state values on these.

func (*ExampleApp) Preferences

func (a *ExampleApp) Preferences() (preferences.AppPreferences, error)

Preferences sets up and returns our app preferences. This will load our preferences from disk. If no preferences file exists, default preferences will be set (as would be the case on first run).

func (*ExampleApp) States

func (a *ExampleApp) States() []*mqttapi.Msg

States is called when we want to send our sensor data to Home Assistant.

func (*ExampleApp) Subscriptions

func (a *ExampleApp) Subscriptions() []*mqttapi.Subscription

Subscriptions is called once to register the callbacks we will use when Home Assistant sends back messages on any command topics.

func (*ExampleApp) URL

func (a *ExampleApp) URL() string

In order to use the web.ExecuteRequest helper to fetch the weather, we need to pass it a type that satisfies the web.Request interface. We can do this by adding a URL() method that returns the URL to our weather provider, to our app struct.

func (*ExampleApp) UnmarshalJSON

func (a *ExampleApp) UnmarshalJSON(data []byte) error

We also need a way to save the response of the web request, and we can do this by satisfying the web.Response interface through adding a UnmarshalJSON that will take the raw response JSON and save it into our app struct.

func (*ExampleApp) Update

func (a *ExampleApp) Update(ctx context.Context) error

Run is the function that the agent calls to start our app. In it, we create our app struct, register our app (if needed), listen for our button press, then set up a loop to send our sensor data.

Jump to

Keyboard shortcuts

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