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

feat: switch to WASM #2

Merged
merged 1 commit into from
Oct 22, 2023
Merged
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
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
- package-ecosystem: gomod
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: weekly
open-pull-requests-limit: 10
38 changes: 38 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
on:
push:
tags:
- '*'

name: release
jobs:
release:

runs-on: ubuntu-latest
env:
GO_VERSION: "1.21"

steps:
- name: Install Go
if: success()
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Checkout code
uses: actions/checkout@v4

- name: Cache Go modules
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
on:
push:
branches:
- main
pull_request:

name: run tests
jobs:
test:

runs-on: ubuntu-latest
env:
GOOS: js
GOARCH: wasm
GO_VERSION: "1.21"
GOLANGCI_LINT_VERSION: v1.55.0

steps:
- name: Install Go
if: success()
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Checkout code
uses: actions/checkout@v4

- name: Cache Go modules
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-

- name: Run linter
uses: golangci/golangci-lint-action@v3
with:
version: ${{ env.GOLANGCI_LINT_VERSION }}
skip-pkg-cache: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist
35 changes: 21 additions & 14 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
run:
tests: false
timeout: 5m
deadline: 5m

linters-settings:
cyclop:
max-complexity: 20
skip-tests: true
funlen:
lines: 80
gofumpt:
extra-rules: true

linters:
enable-all: true
disable:
- interfacebloat
- sqlclosecheck # not relevant (SQL)
- rowserrcheck # not relevant (SQL)
- execinquery # not relevant (SQL)
- interfacer # deprecated
- scopelint # deprecated
- maligned # deprecated
- golint # deprecated
- durationcheck
- deadcode # deprecated
- exhaustivestruct # deprecated
- ifshort # deprecated
- nosnakecase # deprecated
- structcheck # deprecated
- varcheck # deprecated
- cyclop # duplicate of gocyclo
- depguard
- exhaustive
- exhaustivestruct
- exhaustruct
- forcetypeassert
- funlen
- gochecknoglobals
- gochecknoinits
- gocognit
- gocyclo
- goerr113
- gomnd
- ireturn
- nestif
- nlreturn
- nilerr
- noctx
- nonamedreturns
- tagliatelle
- varnamelen
- wrapcheck
- wsl

issues:
exclude-use-default: false
exclude:
- 'ST1000: at least one file in a package should have a package comment'
exclude-rules:
- path: module/client.go
linters:
- noctx
- 'package-comments: should have a package comment'
35 changes: 35 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
project_name: hass-floorplan
dist: dist

gomod:
proxy: true

builds:
- main: ./
binary: "{{ .ProjectName }}"
goos:
- js
goarch:
- wasm
env:
- CGO_ENABLED=0

archives:
- format: binary
name_template: '{{ .Binary }}'

changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^doc:'
- '^tests:'
- '^test:'
- '^chore:'

checksum:
name_template: '{{ .ProjectName }}_checksums.txt'

snapshot:
name_template: "{{ .Tag }}"
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
GOOS=js
GOARCH=wasm

export GOOS
export GOARCH

# Format all files
fmt:
@echo "==> Formatting source"
@gofmt -s -w $(shell find . -type f -name '*.go' -not -path "./vendor/*")
@echo "==> Done"
.PHONY: fmt

# Tidy the go.mod file
tidy:
@echo "==> Cleaning go.mod"
@go mod tidy
@echo "==> Done"
.PHONY: tidy

# Lint the project
lint:
@echo "==> Linting Go files"
@golangci-lint run ./...
.PHONY: lint

# Run all tests
test:
@go test -cover ./...
.PHONY: test

# Build the commands
build:
@goreleaser release --clean --snapshot
.PHONY: build

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -11,11 +11,11 @@ to under modules in your configuration.

```yaml
modules:
- name: floorplan
path: github.com/glasslabs/hass-floorplan
- name: floorplan
url: https://github.com/glasslabs/hass-floorplan/releases/download/v1.0.0/hass-floorplan.wasm
position: top:right
config:
url: http://my-hass-instance:8123/
url: http://my-hass-instance:8123
token: <your-hass-token>
floorplan: {{ .ConfigPath }}/assets/floorplan.svg
mapping:
168 changes: 0 additions & 168 deletions client.go

This file was deleted.

14 changes: 11 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module github.com/glasslabs/floorplan
module github.com/glasslabs/hass-floorplan

go 1.16
go 1.21.3

require github.com/glasslabs/looking-glass v0.1.0-alpha1
require (
github.com/glasslabs/client-go v0.1.0
github.com/pawal/go-hass v0.0.0-20230221123149-b1b116a7432d
)

require (
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/js/dom/v2 v2.0.0-20230808055721-96db8f4d5e3b // indirect
)
33 changes: 9 additions & 24 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/glasslabs/looking-glass v0.1.0-alpha1 h1:cfqUVMSy0trnPDUqugS74ClF063zQ/IEN7sZyhIGLvs=
github.com/glasslabs/looking-glass v0.1.0-alpha1/go.mod h1:uZHdBnmcltBmrCHBIrmoKxskSMAmli6/1lOcIS5PoMs=
github.com/hamba/logger v1.0.1/go.mod h1:rpB9y29AN0sHvhc7QfzJZxgJFqh536/nZIFiVhqLkuE=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/traefik/yaegi v0.9.2/go.mod h1:FAYnRlZyuVlEkvnkHq3bvJ1lW5be6XuwgLdkYgYG6Lk=
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/vincent-petithory/dataurl v0.0.0-20191104211930-d1553a71de50/go.mod h1:FHafX5vmDzyP+1CQATJn7WFKc9CvnvxyvZy6I1MrG/U=
github.com/zserge/lorca v0.1.9/go.mod h1:bVmnIbIRlOcoV285KIRSe4bUABKi7R7384Ycuum6e4A=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
github.com/glasslabs/client-go v0.1.0 h1:a2Ob6EMyglz+Jy53diQv62ZCBVA4/BONF3e2APcnlr0=
github.com/glasslabs/client-go v0.1.0/go.mod h1:CpO4gMLfNrbhZQsNlNjq1KcGUAk35eCWj35YBb2xccw=
github.com/pawal/go-hass v0.0.0-20230221123149-b1b116a7432d h1:8tAKssHhfrcb3zHE/EpS+p3fYUk4RLROOGoPba6/tHs=
github.com/pawal/go-hass v0.0.0-20230221123149-b1b116a7432d/go.mod h1:dEToidnncZjw4CqHXSpE0KI17uDI86Gt0Gfp5PEJKyA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/js/dom/v2 v2.0.0-20230808055721-96db8f4d5e3b h1:XOEHdukvK2DAtBpN8kQbuj6UIK5dz9DLvqc51o6w4L0=
honnef.co/go/js/dom/v2 v2.0.0-20230808055721-96db8f4d5e3b/go.mod h1:+JtEcbinwR4znM12aluJ3WjKgvhDPKPQ8hnP4YM+4jI=
215 changes: 0 additions & 215 deletions hass.go

This file was deleted.

171 changes: 171 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
//go:build js && wasm

package main

import (
_ "embed"
"fmt"
"strings"
"time"

"github.com/glasslabs/client-go"
"github.com/pawal/go-hass"
)

const htmlWrapper = `<div class="hass-floorplan">%s</div>`

//go:embed assets/style.css
var css []byte

// Config is the module configuration.
type Config struct {
URL string `yaml:"url"`
Token string `yaml:"token"`
Floorplan string `yaml:"floorplan"`
Mapping map[string]string `yaml:"mapping"`
}

// NewConfig creates a default configuration for the module.
func NewConfig() *Config {
return &Config{}
}

func main() {
log := client.NewLogger()
mod, err := client.NewModule()
if err != nil {
log.Error("Could not create module", "error", err.Error())
return
}

cfg := NewConfig()
if err = mod.ParseConfig(&cfg); err != nil {
log.Error("Could not parse config", "error", err.Error())
return
}

log.Info("Loading Module", "module", mod.Name())

m := &Module{
mod: mod,
cfg: cfg,
log: log,
}

if err = m.setup(); err != nil {
log.Error("Could not setup module", "error", err.Error())
return
}

first := true
for {
if !first {
time.Sleep(10 * time.Second)
}
first = false

if err = m.syncStates(); err != nil {
log.Error("Could not sync states", "error", err.Error())
continue
}

if err = m.listenStates(); err != nil {
log.Error("Could not listen to states", "error", err.Error())
continue
}
}
}

// Module runs the module.
type Module struct {
mod *client.Module
cfg *Config

ha *hass.Access

log *client.Logger
}

func (m *Module) setup() error {
if err := m.mod.LoadCSS(string(css)); err != nil {
return fmt.Errorf("loading css: %w", err)
}

svg, err := m.mod.Asset(m.cfg.Floorplan)
if err != nil {
return fmt.Errorf("could not read floorplan image: %w", err)
}
m.mod.Element().SetInnerHTML(fmt.Sprintf(htmlWrapper, string(svg)))

ha := hass.NewAccess(m.cfg.URL, "")
ha.SetBearerToken(m.cfg.Token)
if err := ha.CheckAPI(); err != nil {
return fmt.Errorf("could not connect to home assistant: %w", err)
}
m.ha = ha

return nil
}

func (m *Module) syncStates() error {
states, err := m.ha.FilterStates("light", "switch", "cover", "binary_sensor")
if err != nil {
return fmt.Errorf("getting states: %w", err)
}

for _, state := range states {
m.updateState(state.EntityID, state.State)
}
return nil
}

func (m *Module) listenStates() error {
l, err := m.ha.ListenEvents()
if err != nil {
return fmt.Errorf("calling listen: %w", err)
}
defer func() { _ = l.Close() }()

for {
event, err := l.NextStateChanged()
if err != nil {
return fmt.Errorf("listening for event: %w", err)
}

if event.EventType != "state_changed" {
continue
}
switch strings.TrimSuffix(strings.SplitAfter(event.Data.EntityID, ".")[0], ".") {
case "light", "switch", "cover", "binary_sensor":
default:
continue
}

m.updateState(event.Data.EntityID, event.Data.NewState.State)
}
}

func (m *Module) updateState(id, state string) {
if mapping, ok := m.cfg.Mapping[id]; ok {
id = mapping
}
id = strings.ReplaceAll(id, ".", "\\.")

actualState := "unavailable"
if state != "unavailable" && state != "unknown" {
actualState = "off"
if state == "on" || state == "open" {
actualState = "on"
}
}

elem := m.mod.Element().QuerySelector("#" + id)
if elem == nil {
return
}

elem.Class().Remove("on")
elem.Class().Remove("off")
elem.Class().Remove("unavailable")
elem.Class().Add(actualState)
}