|
| 1 | +package homeassistant |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + |
| 8 | + // Packages |
| 9 | + agent "github.com/mutablelogic/go-client/pkg/agent" |
| 10 | +) |
| 11 | + |
| 12 | +/////////////////////////////////////////////////////////////////////////////// |
| 13 | +// TYPES |
| 14 | + |
| 15 | +type tool struct { |
| 16 | + name string |
| 17 | + description string |
| 18 | + params []agent.ToolParameter |
| 19 | + run func(context.Context, *agent.ToolCall) (*agent.ToolResult, error) |
| 20 | +} |
| 21 | + |
| 22 | +// Ensure tool satisfies the agent.Tool interface |
| 23 | +var _ agent.Tool = (*tool)(nil) |
| 24 | + |
| 25 | +/////////////////////////////////////////////////////////////////////////////// |
| 26 | +// PUBLIC METHODS |
| 27 | + |
| 28 | +// Return all the agent tools for the weatherapi |
| 29 | +func (c *Client) Tools() []agent.Tool { |
| 30 | + return []agent.Tool{ |
| 31 | + &tool{ |
| 32 | + name: "devices", |
| 33 | + description: "Lookup all device id's in the home, or search for a device ny name", |
| 34 | + run: c.agentGetDeviceIds, |
| 35 | + params: []agent.ToolParameter{ |
| 36 | + { |
| 37 | + Name: "name", |
| 38 | + Description: "Name to filter devices", |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, &tool{ |
| 42 | + name: "get_device_state", |
| 43 | + description: "Return the current state of a device, given the device id", |
| 44 | + run: c.agentGetDeviceState, |
| 45 | + params: []agent.ToolParameter{ |
| 46 | + { |
| 47 | + Name: "device", |
| 48 | + Description: "The device id", |
| 49 | + Required: true, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/////////////////////////////////////////////////////////////////////////////// |
| 57 | +// PRIVATE METHODS - TOOL |
| 58 | + |
| 59 | +func (*tool) Provider() string { |
| 60 | + return "homeassistant" |
| 61 | +} |
| 62 | + |
| 63 | +func (t *tool) Name() string { |
| 64 | + return t.name |
| 65 | +} |
| 66 | + |
| 67 | +func (t *tool) Description() string { |
| 68 | + return t.description |
| 69 | +} |
| 70 | + |
| 71 | +func (t *tool) Params() []agent.ToolParameter { |
| 72 | + return t.params |
| 73 | +} |
| 74 | + |
| 75 | +func (t *tool) Run(ctx context.Context, call *agent.ToolCall) (*agent.ToolResult, error) { |
| 76 | + return t.run(ctx, call) |
| 77 | +} |
| 78 | + |
| 79 | +/////////////////////////////////////////////////////////////////////////////// |
| 80 | +// PRIVATE METHODS - TOOL |
| 81 | + |
| 82 | +var ( |
| 83 | + allowedClasses = []string{ |
| 84 | + "temperature", |
| 85 | + "humidity", |
| 86 | + "battery", |
| 87 | + "select", |
| 88 | + "number", |
| 89 | + "switch", |
| 90 | + "enum", |
| 91 | + "light", |
| 92 | + "sensor", |
| 93 | + "binary_sensor", |
| 94 | + "remote", |
| 95 | + "climate", |
| 96 | + "occupancy", |
| 97 | + "motion", |
| 98 | + "button", |
| 99 | + "door", |
| 100 | + "lock", |
| 101 | + "tv", |
| 102 | + "vacuum", |
| 103 | + } |
| 104 | +) |
| 105 | + |
| 106 | +// Return the current devices and their id's |
| 107 | +func (c *Client) agentGetDeviceIds(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) { |
| 108 | + name, err := call.String("name") |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + // Query all devices |
| 114 | + devices, err := c.States() |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + // Make the device id's |
| 120 | + type DeviceId struct { |
| 121 | + Id string `json:"id"` |
| 122 | + Name string `json:"name"` |
| 123 | + } |
| 124 | + var result []DeviceId |
| 125 | + for _, device := range devices { |
| 126 | + if !slices.Contains(allowedClasses, device.Class()) { |
| 127 | + continue |
| 128 | + } |
| 129 | + var found bool |
| 130 | + if name != "" { |
| 131 | + if strings.Contains(strings.ToLower(device.Name()), strings.ToLower(name)) { |
| 132 | + found = true |
| 133 | + } else if strings.Contains(strings.ToLower(device.Class()), strings.ToLower(name)) { |
| 134 | + found = true |
| 135 | + } |
| 136 | + if !found { |
| 137 | + continue |
| 138 | + } |
| 139 | + } |
| 140 | + result = append(result, DeviceId{ |
| 141 | + Id: device.Entity, |
| 142 | + Name: device.Name(), |
| 143 | + }) |
| 144 | + } |
| 145 | + return &agent.ToolResult{ |
| 146 | + Id: call.Id, |
| 147 | + Result: map[string]any{ |
| 148 | + "type": "text", |
| 149 | + "devices": result, |
| 150 | + }, |
| 151 | + }, nil |
| 152 | +} |
| 153 | + |
| 154 | +// Return a device state |
| 155 | +func (c *Client) agentGetDeviceState(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) { |
| 156 | + device, err := call.String("device") |
| 157 | + if err != nil { |
| 158 | + return nil, err |
| 159 | + } |
| 160 | + |
| 161 | + state, err := c.State(device) |
| 162 | + if err != nil { |
| 163 | + return nil, err |
| 164 | + } |
| 165 | + |
| 166 | + return &agent.ToolResult{ |
| 167 | + Id: call.Id, |
| 168 | + Result: map[string]any{ |
| 169 | + "type": "text", |
| 170 | + "device": state, |
| 171 | + }, |
| 172 | + }, nil |
| 173 | +} |
0 commit comments