| 1 |
package homeassistant |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net" |
| 6 |
"net/url" |
| 7 |
"path" |
| 8 |
"strconv" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 13 |
) |
| 14 |
|
| 15 |
// Config holds settings for the Home Assistant notification service. |
| 16 |
type Config struct { |
| 17 |
// Token is the long-lived access token sent as a Bearer credential. |
| 18 |
Token string `desc:"Long-lived access token" url:"user"` |
| 19 |
// Host is the Home Assistant hostname. |
| 20 |
Host string `desc:"Home Assistant hostname" url:"host"` |
| 21 |
// Port is the Home Assistant port. When omitted, HTTPS uses 443 and HTTP uses 8123. |
| 22 |
Port int `desc:"Home Assistant port" optional:"" url:"port"` |
| 23 |
// Path is an optional reverse-proxy URL prefix. |
| 24 |
Path string `desc:"Reverse-proxy path prefix" optional:"" url:"path"` |
| 25 |
// Title is an optional notification title. It is omitted from the API request when empty. |
| 26 |
Title string `desc:"Notification title" key:"title" optional:""` |
| 27 |
// Service is the Home Assistant action to call. Empty defaults to persistent_notification.create. |
| 28 |
Service string `desc:"Home Assistant action (domain.action or notify target)" key:"service" optional:""` |
| 29 |
// Targets are notify-platform destination names. They are omitted for persistent notifications. |
| 30 |
Targets []string `desc:"Notify targets" key:"targets" optional:""` |
| 31 |
// Nid is the persistent notification ID. It is omitted when empty. |
| 32 |
Nid string `desc:"Persistent notification ID" key:"nid" optional:""` |
| 33 |
// DisableTLS sends the request over HTTP instead of HTTPS. |
| 34 |
DisableTLS bool `default:"No" desc:"Disable TLS (use HTTP)" key:"disabletls"` |
| 35 |
// SkipTLSVerify skips TLS certificate verification. |
| 36 |
SkipTLSVerify bool `default:"No" desc:"Skip TLS certificate verification" key:"skiptlsverify"` |
| 37 |
} |
| 38 |
|
| 39 |
const ( |
| 40 |
// Scheme identifies this service in configuration URLs. |
| 41 |
Scheme = "homeassistant" |
| 42 |
|
| 43 |
// dummyServiceURL is the placeholder URL used by documentation generation. |
| 44 |
dummyServiceURL = "homeassistant://[email protected]" |
| 45 |
|
| 46 |
// defaultTLSPort is used when the port is omitted and TLS is enabled. |
| 47 |
defaultTLSPort = 443 |
| 48 |
|
| 49 |
// defaultHTTPPort is used when the port is omitted and TLS is disabled. |
| 50 |
defaultHTTPPort = 8123 |
| 51 |
|
| 52 |
// persistentDomain is the Home Assistant domain for persistent notifications. |
| 53 |
persistentDomain = "persistent_notification" |
| 54 |
|
| 55 |
// persistentService is the Home Assistant action that creates a persistent notification. |
| 56 |
persistentService = "create" |
| 57 |
|
| 58 |
// notifyDomain is the default Home Assistant domain when service has no dot. |
| 59 |
notifyDomain = "notify" |
| 60 |
) |
| 61 |
|
| 62 |
// Enums returns the fields that use an EnumFormatter for their values. |
| 63 |
// |
| 64 |
// Returns: |
| 65 |
// - An empty map because this service has no enum fields. |
| 66 |
func (*Config) Enums() map[string]types.EnumFormatter { |
| 67 |
return map[string]types.EnumFormatter{} |
| 68 |
} |
| 69 |
|
| 70 |
// GetURL returns a URL representation of the current configuration. |
| 71 |
// |
| 72 |
// Returns: |
| 73 |
// - The service URL encoding the current field values. |
| 74 |
func (c *Config) GetURL() *url.URL { |
| 75 |
resolver := format.NewPropKeyResolver(c) |
| 76 |
|
| 77 |
return c.getURL(&resolver) |
| 78 |
} |
| 79 |
|
| 80 |
// SetURL updates the configuration from a URL representation. |
| 81 |
// |
| 82 |
// Parameters: |
| 83 |
// - serviceURL: The service URL to parse. |
| 84 |
// |
| 85 |
// Returns: |
| 86 |
// - An error if the URL is invalid or required fields are missing. |
| 87 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 88 |
resolver := format.NewPropKeyResolver(c) |
| 89 |
|
| 90 |
return c.setURL(&resolver, serviceURL) |
| 91 |
} |
| 92 |
|
| 93 |
// apiURL returns the absolute REST URL for a Home Assistant service call. |
| 94 |
// |
| 95 |
// Parameters: |
| 96 |
// - domain: The Home Assistant domain. |
| 97 |
// - service: The Home Assistant action. |
| 98 |
// |
| 99 |
// Returns: |
| 100 |
// - The absolute POST URL. |
| 101 |
func (c *Config) apiURL(domain, service string) string { |
| 102 |
scheme := "https" |
| 103 |
if c.DisableTLS { |
| 104 |
scheme = "http" |
| 105 |
} |
| 106 |
|
| 107 |
apiPath := path.Join(c.Path, "api", "services", domain, service) |
| 108 |
if apiPath == "" || apiPath[0] != '/' { |
| 109 |
apiPath = "/" + apiPath |
| 110 |
} |
| 111 |
|
| 112 |
return (&url.URL{ |
| 113 |
Scheme: scheme, |
| 114 |
Host: net.JoinHostPort(c.Host, strconv.Itoa(c.requestPort())), |
| 115 |
Path: apiPath, |
| 116 |
}).String() |
| 117 |
} |
| 118 |
|
| 119 |
// domainService resolves the Home Assistant domain and action from Service. |
| 120 |
// |
| 121 |
// Returns: |
| 122 |
// - domain: The Home Assistant domain. |
| 123 |
// - service: The Home Assistant action. |
| 124 |
// - err: An error if Service is set but not a valid domain.action pair. |
| 125 |
func (c *Config) domainService() (string, string, error) { |
| 126 |
if c.Service == "" { |
| 127 |
return persistentDomain, persistentService, nil |
| 128 |
} |
| 129 |
|
| 130 |
if !strings.Contains(c.Service, ".") { |
| 131 |
if !validHASlug(c.Service) { |
| 132 |
return "", "", fmt.Errorf("%w: %q", ErrInvalidService, c.Service) |
| 133 |
} |
| 134 |
|
| 135 |
return notifyDomain, c.Service, nil |
| 136 |
} |
| 137 |
|
| 138 |
domain, service, _ := strings.Cut(c.Service, ".") |
| 139 |
if !validHASlug(domain) || !validHASlug(service) { |
| 140 |
return "", "", fmt.Errorf("%w: %q", ErrInvalidService, c.Service) |
| 141 |
} |
| 142 |
|
| 143 |
return domain, service, nil |
| 144 |
} |
| 145 |
|
| 146 |
// getURL constructs a service URL from the current configuration. |
| 147 |
// |
| 148 |
// Parameters: |
| 149 |
// - resolver: Resolver used to encode query parameters from config fields. |
| 150 |
// |
| 151 |
// Returns: |
| 152 |
// - The service URL with the token as userinfo and the host as hostname. |
| 153 |
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 154 |
host := c.Host |
| 155 |
if c.Port != 0 && c.Port != c.impliedPort() { |
| 156 |
host = net.JoinHostPort(c.Host, strconv.Itoa(c.Port)) |
| 157 |
} |
| 158 |
|
| 159 |
return &url.URL{ |
| 160 |
Scheme: Scheme, |
| 161 |
User: url.User(c.Token), |
| 162 |
Host: host, |
| 163 |
Path: c.Path, |
| 164 |
RawQuery: format.BuildQuery(resolver), |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// impliedPort returns the port used when the URL omits one. |
| 169 |
// |
| 170 |
// Returns: |
| 171 |
// - 8123 when TLS is disabled, otherwise 443. |
| 172 |
func (c *Config) impliedPort() int { |
| 173 |
if c.DisableTLS { |
| 174 |
return defaultHTTPPort |
| 175 |
} |
| 176 |
|
| 177 |
return defaultTLSPort |
| 178 |
} |
| 179 |
|
| 180 |
// isPersistent reports whether the resolved action creates a persistent notification. |
| 181 |
// |
| 182 |
// Returns: |
| 183 |
// - true when the action is persistent_notification.create. |
| 184 |
func (c *Config) isPersistent() bool { |
| 185 |
domain, service, err := c.domainService() |
| 186 |
if err != nil { |
| 187 |
return false |
| 188 |
} |
| 189 |
|
| 190 |
return domain == persistentDomain && service == persistentService |
| 191 |
} |
| 192 |
|
| 193 |
// requestPort returns the TCP port used for the API request. |
| 194 |
// |
| 195 |
// Returns: |
| 196 |
// - The configured port, or the implied default when the port was omitted. |
| 197 |
func (c *Config) requestPort() int { |
| 198 |
if c.Port != 0 { |
| 199 |
return c.Port |
| 200 |
} |
| 201 |
|
| 202 |
return c.impliedPort() |
| 203 |
} |
| 204 |
|
| 205 |
// setPort parses an optional TCP port from the URL. |
| 206 |
// |
| 207 |
// Parameters: |
| 208 |
// - rawPort: The port string from the URL, or empty when omitted. |
| 209 |
// |
| 210 |
// Returns: |
| 211 |
// - An error if the port is not a valid integer in 1–65535. |
| 212 |
func (c *Config) setPort(rawPort string) error { |
| 213 |
if rawPort == "" { |
| 214 |
c.Port = 0 |
| 215 |
|
| 216 |
return nil |
| 217 |
} |
| 218 |
|
| 219 |
port, err := strconv.Atoi(rawPort) |
| 220 |
if err != nil || port < 1 || port > 65535 { |
| 221 |
return fmt.Errorf("%w: %q", ErrInvalidPort, rawPort) |
| 222 |
} |
| 223 |
|
| 224 |
c.Port = port |
| 225 |
|
| 226 |
return nil |
| 227 |
} |
| 228 |
|
| 229 |
// setURL updates the configuration from a service URL. |
| 230 |
// |
| 231 |
// Parameters: |
| 232 |
// - resolver: Resolver used to apply query parameters to config fields. |
| 233 |
// - serviceURL: The service URL to parse. |
| 234 |
// |
| 235 |
// Returns: |
| 236 |
// - An error if a query parameter is invalid or required fields are missing. |
| 237 |
func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 238 |
isDummy := serviceURL.String() == dummyServiceURL |
| 239 |
|
| 240 |
if serviceURL.User != nil { |
| 241 |
c.Token = serviceURL.User.Username() |
| 242 |
} else { |
| 243 |
c.Token = "" |
| 244 |
} |
| 245 |
|
| 246 |
c.Host = serviceURL.Hostname() |
| 247 |
|
| 248 |
if err := c.setPort(serviceURL.Port()); err != nil { |
| 249 |
return err |
| 250 |
} |
| 251 |
|
| 252 |
c.Path = normalizePath(serviceURL.Path) |
| 253 |
|
| 254 |
for key, vals := range serviceURL.Query() { |
| 255 |
if len(vals) == 0 { |
| 256 |
continue |
| 257 |
} |
| 258 |
|
| 259 |
if err := resolver.Set(key, vals[0]); err != nil { |
| 260 |
return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err) |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
if isDummy { |
| 265 |
return nil |
| 266 |
} |
| 267 |
|
| 268 |
if c.Token == "" { |
| 269 |
return ErrTokenMissing |
| 270 |
} |
| 271 |
|
| 272 |
if c.Host == "" { |
| 273 |
return ErrHostMissing |
| 274 |
} |
| 275 |
|
| 276 |
if _, _, err := c.domainService(); err != nil { |
| 277 |
return err |
| 278 |
} |
| 279 |
|
| 280 |
return nil |
| 281 |
} |
| 282 |
|
| 283 |
// normalizePath returns a leading-slash path without a trailing slash. |
| 284 |
// |
| 285 |
// Parameters: |
| 286 |
// - raw: The URL path from the service URL. |
| 287 |
// |
| 288 |
// Returns: |
| 289 |
// - The normalized path, or empty when the path is missing or only slashes. |
| 290 |
func normalizePath(raw string) string { |
| 291 |
trimmed := strings.Trim(raw, "/") |
| 292 |
if trimmed == "" { |
| 293 |
return "" |
| 294 |
} |
| 295 |
|
| 296 |
return "/" + trimmed |
| 297 |
} |
| 298 |
|
| 299 |
// validHASlug reports whether s is a Home Assistant domain or action name. |
| 300 |
// |
| 301 |
// Parameters: |
| 302 |
// - s: The candidate domain or action. |
| 303 |
// |
| 304 |
// Returns: |
| 305 |
// - true when s is non-empty and contains only ASCII letters, digits, and underscores. |
| 306 |
func validHASlug(s string) bool { |
| 307 |
if s == "" { |
| 308 |
return false |
| 309 |
} |
| 310 |
|
| 311 |
for _, r := range s { |
| 312 |
switch { |
| 313 |
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '_': |
| 314 |
continue |
| 315 |
default: |
| 316 |
return false |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
return true |
| 321 |
} |