-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
HomeAssistant: add auto-discovery #25141
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `util/homeassistant/handler.go:36` </location>
<code_context>
+ jsonWrite(w, slices.Sorted(maps.Keys(instances)))
+}
+
+func (h *handler) Home(w http.ResponseWriter, req *http.Request) {
+ home := req.PathValue("home")
+
</code_context>
<issue_to_address>
**suggestion:** Returning HTTP 400 for unknown home may not be the most appropriate status code.
Consider returning HTTP 404 instead of 400 when instanceByName(home) is nil, as this better indicates the resource was not found.
Suggested implementation:
```golang
func (h *handler) Home(w http.ResponseWriter, req *http.Request) {
home := req.PathValue("home")
mu.Lock()
defer mu.Unlock()
instance := instanceByName(home)
if instance == nil {
http.Error(w, "home not found", http.StatusNotFound)
return
}
// ... rest of the handler logic ...
```
If the handler previously returned HTTP 400 for this case, you should remove or replace that code with the new block above. Also, ensure that `instanceByName` is accessible in this scope and that the rest of the handler logic is executed only if the instance is found.
</issue_to_address>
### Comment 2
<location> `util/homeassistant/handler.go:64-68` </location>
<code_context>
+}
+
+// jsonWrite writes a JSON response
+func jsonWrite(w http.ResponseWriter, data any) {
+ if err := json.NewEncoder(w).Encode(data); err != nil {
+ log.ERROR.Printf("homeassistant: failed to encode JSON: %v", err)
</code_context>
<issue_to_address>
**suggestion:** Consider setting Content-Type header for JSON responses.
Setting the header in jsonWrite will help clients reliably parse the response as JSON.
```suggestion
func jsonWrite(w http.ResponseWriter, data any) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(data); err != nil {
log.ERROR.Printf("homeassistant: failed to encode JSON: %v", err)
}
}
```
</issue_to_address>
### Comment 3
<location> `server/service/registry.go:36` </location>
<code_context>
+ return mux
+}
+
+type handler struct {
+ base string
+}
</code_context>
<issue_to_address>
**issue (complexity):** Consider replacing the custom handler and manual path logic with ServeMux prefix matching and http.StripPrefix for simpler routing.
Using a custom `ServeHTTP` + global registry + manual path‐trimming adds a lot of boilerplate. You can get identical behavior by wiring each registered handler into a sub‐mux with `ServeMux`’s built-in prefix matching and `http.StripPrefix`. For example, drop the `handler` type entirely and replace your `Handler` with:
```go
func Handler(base string) http.Handler {
mux := http.NewServeMux()
mu.Lock()
defer mu.Unlock()
for name, h := range registry {
// e.g. "/homes/foo/"
prefix := fmt.Sprintf("%s/%s/", base, name)
// strip "/homes/foo" then hand off to h
mux.Handle(prefix, http.StripPrefix(prefix, h))
}
return mux
}
```
Then you can remove:
- the custom `handler` struct
- its `ServeHTTP` + manual `TrimPrefix` call
- the dynamic path‐param logic
All you need is the global `registry` + `Register` and a single loop at startup, no hand‐rolled routing or braces parsing.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
@naltatis network part is finished |
|
Doesn't build: |
# Conflicts: # api/error.go
# Conflicts: # api/error.go # plugin/auth/oauth.go
|
@andig neue npm dependency. mach mal |
|
@naltatis added the |
not at first glance. Seems to primarily affect config UI related tests. |
|
@andig seems to be This also explains the strange behavior you showed me, where the ui does not show newly created devices. |
|
Finetuned autocomplete/datalist behavior across browsers. 🔽 show dropdown icon if options are available example in desktop Safari Bildschirmaufnahme.2025-11-18.um.17.49.21.webmexample iOS Safari mobile.webm |
Depends on #25285, #25342
TODO