Skip to content

Commit

Permalink
one-step flow submission, fuck you homeassistant
Browse files Browse the repository at this point in the history
  • Loading branch information
asymmetricia committed Dec 19, 2023
1 parent 920d3cf commit cccaead
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 9 deletions.
35 changes: 29 additions & 6 deletions api/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"encoding/json"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -129,9 +130,9 @@ type ConfigFlow struct {
Type string `json:"type"`

// These fields appear in the response to SetFlow, maybe based on `Type`?
Description string `json:"description,omitempty"`
Result string `json:"result,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Title string `json:"title,omitempty"`
}

type ConfigFlowDataSchema struct {
Expand Down Expand Up @@ -187,10 +188,16 @@ func (c *Client) SetFlow(id ConfigFlowId, payload map[string]interface{}) (resul
return "", fmt.Errorf("server responsed with error: %s", strings.Join(errs, "; "))
}

if flow.Result != "" {
return flow.Result, nil
if len(flow.Result) == 0 {
return "", errors.New("flow Result was empty")
}
return "", errors.New("flow Result was empty")

var s string
if err := json.Unmarshal(flow.Result, &s); err == nil {
return s, nil
}

return string(flow.Result), nil
}

func (c *Client) StartFlow(handler string) (*ConfigFlow, error) {
Expand Down Expand Up @@ -240,3 +247,19 @@ func (c *Client) ListFlowHandlers() ([]string, error) {
}
return handlers.([]string), nil
}

type DeleteEntryResponse struct {
RequireRestart bool `json:"require_restart"`
}

func (c *Client) DeleteEntry(id EntryId) (*DeleteEntryResponse, error) {
res, err := c.RawRESTDeleteAs(
fmt.Sprintf("api/config/config_entries/entry/%s", string(id)),
nil,
(*DeleteEntryResponse)(nil),
)
if err != nil {
return nil, err
}
return res.(*DeleteEntryResponse), nil
}
2 changes: 1 addition & 1 deletion api/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func convert(obj interface{}, prototype interface{}, err error) (interface{}, er
ob, _ := json.Marshal(obj)
ret := reflect.New(reflect.TypeOf(prototype)).Interface()
if err := json.Unmarshal(ob, &ret); err != nil {
return nil, fmt.Errorf("could not convert response to %T: %v", ret, err)
return nil, fmt.Errorf("could not convert response %q to %T: %v", ob, ret, err)
}

return reflect.Indirect(reflect.ValueOf(ret)).Interface(), nil
Expand Down
62 changes: 60 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ var configSetFlowCmd = &cobra.Command{
Short: "set the given key-value pairs on the flow with the given ID",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
handler, _ := cmd.Flags().GetBool("handler")

if handler {
ret, err := client(cmd).StartFlow(args[0])
if err != nil {
logrus.WithError(err).Fatal("start-flow failed")
}
logrus.Debugf("started flow %+v", ret)
args[0] = string(ret.FlowId)
}

log := logrus.WithField("flow_id", args[0])
id := api.ConfigFlowId(args[0])
kv := map[string]string{}
Expand Down Expand Up @@ -161,6 +172,34 @@ var configStartOptionsFlowCmd = &cobra.Command{
},
}

var configStartFlowCmd = &cobra.Command{
Use: "start-flow [handler-name]",
Short: "start a config flow with with the given handler name as the handler, e.g., to configure a new integration",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ret, err := client(cmd).StartFlow(args[0])
if err != nil {
logrus.WithError(err).Fatal("start-flow failed")
}
retJson, _ := json.Marshal(ret)
fmt.Println(string(retJson))
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
entries, err := client(cmd).ListFlowHandlers()
if err != nil {
logrus.WithError(err).Error("could not list config entries")
return nil, cobra.ShellCompDirectiveError
}
var ret []string
for _, entry := range entries {
if strings.HasPrefix(entry, toComplete) {
ret = append(ret, entry)
}
}
return ret, cobra.ShellCompDirectiveNoFileComp
},
}

var configGetOptionsFlowCmd = &cobra.Command{
Use: "get-options-flow [flow-id]",
Short: "retrieve the current state of the indicated options flow",
Expand Down Expand Up @@ -203,14 +242,33 @@ var configListFlowHandlersCmd = &cobra.Command{
},
}

var configDeleteEntryCmd = &cobra.Command{
Use: "delete-entry [entry-id]",
Short: "delete the entry with the given ID",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ret, err := client(cmd).DeleteEntry(api.EntryId(args[0]))
if err != nil {
logrus.WithError(err).Fatal("could not delete entry")
}
retJson, _ := json.Marshal(ret)
fmt.Println(string(retJson))
},
}

func init() {
configCmd.AddCommand(configListEntriesCmd,
configSetFlowCmd.Flags().Bool("handler", false, "if true, flow-id is interpreted as a handler "+
"name, and the handler is started and then set in a single step")
configCmd.AddCommand(
configListEntriesCmd,
configListFlowHandlersCmd,
configListFlowsCmd,
configGetFlowCmd,
configGetConfigCmd,
configGetOptionsFlowCmd,
configSetFlowCmd,
configStartOptionsFlowCmd)
configStartFlowCmd,
configStartOptionsFlowCmd,
configDeleteEntryCmd)
Root.AddCommand(configCmd)
}

0 comments on commit cccaead

Please sign in to comment.