From 256ca8c9a81e660cf479e0d7193bd24e1c9040ed Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Fri, 23 Aug 2024 20:42:03 -0600 Subject: [PATCH] get basic functionality worknig --- .gitignore | 1 + FyneApp.toml | 2 +- flake.nix | 31 +++++---- main.go | 176 +++++++++++++++++++++++++++++++-------------------- 4 files changed, 127 insertions(+), 83 deletions(-) diff --git a/.gitignore b/.gitignore index 0446835..a829d45 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.bak result tags +fass \ No newline at end of file diff --git a/FyneApp.toml b/FyneApp.toml index 6b0f5ab..e94f192 100644 --- a/FyneApp.toml +++ b/FyneApp.toml @@ -2,6 +2,6 @@ Website = "https://github.com/qbit/fass" [Details] Icon = "toggleswitch.png" -Name = "FyneHome" +Name = "fass" ID = "dev.suah.fass" Version = "1.0.0" diff --git a/flake.nix b/flake.nix index 344f1a0..04344a5 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,5 @@ { - description = "thing: stuff and things"; + description = "fass: stuff and fass"; inputs.nixpkgs.url = "nixpkgs/nixos-unstable"; @@ -14,22 +14,23 @@ nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; }); in { - overlay = _: prev: { inherit (self.packages.${prev.system}) thing; }; + overlay = _: prev: { inherit (self.packages.${prev.system}) fass; }; packages = forAllSystems (system: let pkgs = nixpkgsFor.${system}; in { - thing = with pkgs; pkgs.buildGoModule { - pname = "thing"; + fass = with pkgs; pkgs.buildGoModule rec { + pname = "fass"; version = "v0.0.0"; src = ./.; - vendorHash = pkgs.lib.fakeSha256; + vendorHash = "sha256-RY8ExxmgfKdEcmV8FLM8mhr/CKAL3pPjgzW7zR1HCv4="; nativeBuildInputs = [ pkg-config copyDesktopItems ]; buildInputs = [ + fyne glfw libGL libGLU @@ -44,18 +45,20 @@ xorg.xinput ]; - desktopItems = [ - (makeDesktopItem { - name = "traygent"; - exec = pname; - icon = pname; - desktopName = pname; - }) - ]; + buildPhase = '' + ${fyne}/bin/fyne package + ''; + + installPhase = '' + mkdir -p $out + pkg="$PWD/${pname}.tar.xz" + cd $out + tar --strip-components=1 -xvf $pkg + ''; }; }); - defaultPackage = forAllSystems (system: self.packages.${system}.thing); + defaultPackage = forAllSystems (system: self.packages.${system}.fass); devShells = forAllSystems (system: let pkgs = nixpkgsFor.${system}; diff --git a/main.go b/main.go index bd8b9d1..1f6891a 100644 --- a/main.go +++ b/main.go @@ -2,24 +2,41 @@ package main import ( "fmt" + "io" "log" - "os" "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" + "fyne.io/fyne/v2/dialog" "fyne.io/fyne/v2/driver/desktop" + "fyne.io/fyne/v2/storage" "fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/widget" "github.com/pawal/go-hass" ) -type Device struct { - Group string -} +func loadData(h *hass.Access, lightCards *[]fyne.CanvasObject, switchCards *[]fyne.CanvasObject) { + lights, err := h.FilterStates("light") + if err != nil { + log.Fatalln(err) + } + switches, err := h.FilterStates("switch") + if err != nil { + log.Fatalln(err) + } -type State struct { - Devices []Device + for entity := range lights { + e := lights[entity] + card := makeEntity(e, h) + *lightCards = append(*lightCards, card) + } + + for entity := range switches { + e := switches[entity] + card := makeEntity(e, h) + *switchCards = append(*switchCards, card) + } } func getDevice(id string, h *hass.Access) (hass.Device, error) { @@ -54,21 +71,90 @@ func makeEntity(e hass.State, h *hass.Access) *widget.Card { )) } -func main() { - token := os.Getenv("HAAS_TOKEN") - h := hass.NewAccess("https://home.bold.daemon", "") - h.SetBearerToken(token) - err := h.CheckAPI() +func loadSavedData(a fyne.App, input *widget.Entry, file string) { + uri, err := storage.Child(a.Storage().RootURI(), file) if err != nil { - log.Fatalln(err) + return } + reader, err := storage.Reader(uri) + if err != nil { + return + } + defer reader.Close() + + content, err := io.ReadAll(reader) + if err != nil { + return + } + + input.SetText(string(content)) +} + +func saveData(a fyne.App, w fyne.Window, input *widget.Entry, file string) { + uri, err := storage.Child(a.Storage().RootURI(), file) + if err != nil { + dialog.ShowError(err, w) + return + } + writer, err := storage.Writer(uri) + if err != nil { + dialog.ShowError(err, w) + return + } + defer writer.Close() + + _, err = writer.Write([]byte(input.Text)) + if err != nil { + dialog.ShowError(err, w) + return + } + dialog.ShowInformation("Success", "", w) +} + +func main() { a := app.New() - w := a.NewWindow("faas") + w := a.NewWindow("fass") if w == nil { log.Fatalln("unable to create window") } + var lightCards []fyne.CanvasObject + var switchCards []fyne.CanvasObject + + haFile, _ := storage.Child(a.Storage().RootURI(), "haurl") + haExists, _ := storage.Exists(haFile) + tokenFile, _ := storage.Child(a.Storage().RootURI(), "hatoken") + tkExists, _ := storage.Exists(tokenFile) + + urlEntry := widget.NewEntry() + passEntry := widget.NewPasswordEntry() + + loadSavedData(a, urlEntry, "haurl") + loadSavedData(a, passEntry, "hatoken") + + h := hass.NewAccess(urlEntry.Text, "") + if haExists && tkExists { + h.SetBearerToken(passEntry.Text) + err := h.CheckAPI() + if err != nil { + dialog.ShowError(err, w) + } else { + loadData(h, &lightCards, &switchCards) + } + } + + settingsForm := &widget.Form{ + Items: []*widget.FormItem{ + {Text: "Home Assistant URL:", Widget: urlEntry}, + {Text: "Access Token:", Widget: passEntry}, + {Text: "", Widget: widget.NewButton("Save", func() { + saveData(a, w, urlEntry, "haurl") + saveData(a, w, passEntry, "hatoken") + })}, + }, + } + ctrlQ := &desktop.CustomShortcut{KeyName: fyne.KeyQ, Modifier: fyne.KeyModifierControl} ctrlW := &desktop.CustomShortcut{KeyName: fyne.KeyW, Modifier: fyne.KeyModifierControl} w.Canvas().AddShortcut(ctrlQ, func(shortcut fyne.Shortcut) { @@ -78,75 +164,29 @@ func main() { w.Hide() }) - lights, err := h.FilterStates("light") - if err != nil { - log.Fatalln(err) - } - switches, err := h.FilterStates("switch") - if err != nil { - log.Fatalln(err) - } - - var lightCards []fyne.CanvasObject - var switchCards []fyne.CanvasObject - for entity := range lights { - e := lights[entity] - card := makeEntity(e, h) - lightCards = append(lightCards, card) - } - - for entity := range switches { - e := switches[entity] - card := makeEntity(e, h) - switchCards = append(switchCards, card) - } - tabs := container.NewAppTabs( container.NewTabItemWithIcon("Lights", theme.VisibilityIcon(), container.NewVBox( - widget.NewCard("All Lights", "", container.NewVBox( - widget.NewButton("On", func() { - for entity := range lights { - e := lights[entity] - dev, err := getDevice(e.EntityID, h) - if err != nil { - log.Println(err) - return - } - dev.On() - } - }), - widget.NewButton("Off", func() { - for entity := range lights { - e := lights[entity] - dev, err := getDevice(e.EntityID, h) - if err != nil { - log.Println(err) - return - } - dev.Off() - } - }), - )), container.NewAdaptiveGrid(3, lightCards...), )), container.NewTabItemWithIcon("Switches", theme.RadioButtonIcon(), container.NewVBox( - widget.NewCard("All Switches", "", container.NewVBox( - widget.NewButton("On", func() { - - }), - widget.NewButton("Off", func() { - }), - )), container.NewAdaptiveGrid(3, switchCards...), ), ), ) tabs.SetTabLocation(container.TabLocationLeading) - w.SetContent(tabs) + + w.SetContent( + container.NewAppTabs( + container.NewTabItem("Toggles", tabs), + container.NewTabItem("Settings", container.NewStack( + settingsForm, + )), + ), + ) w.SetCloseIntercept(func() { w.Hide() })