saml-dev / gome-assistant

A library that lets you write all of your Home Assistant automations in Go!
https://pkg.go.dev/saml.dev/gome-assistant
Other
81 stars 14 forks source link

Feature Request: go generate auto-discovery of entities and services #9

Open segator opened 1 year ago

segator commented 1 year ago

Hey! I a go developer and I was looking exactly for HA Automation in GO!! I would love to help you in the code but I recentily became father so I don't have free time but I can give you some ideas to improve this project :)

Would be awesome if you use go generate to automatically generate HA Services structs and functions and also to "discover" entities and his properties. this would simplify a lot development of new automations using GA. So you would have auto-complete similar as we have in nodered.

this would help also to detect breaking changes in your automations. Imagine you auto generate the entity mainDoor struct

but you recently changed the name in HA, then as soon you auto generate the code you would see compilation errors. so you would easily detect this brekaing changes!

you could run GA in a docker, and GA itself could try to auto generate himself to detect those errors automatically and notify.

saml-dev commented 1 year ago

I agree this would be really nice, and should be relatively simple to hit the HA api to get all entities and create an Entities file that contains all those constants. I actually maintain a constants file myself in my gome assistant setup, but I agree this would be nice

darrenparkinson commented 6 months ago

I came here to suggest this. I also wondered if this is something a user of the library might create themselves, or if it might be added to the library. It was interesting to read the description relating to codegen for netdaemon. It would also be interesting to know how you're doing it in your constants file?

saml-dev commented 6 months ago

My constants file is literally just constants of strings that I create. It took maybe 20 minutes to copy over all the ones I actually used, and now it's trivial to add one or two when I'm making a new automation. That's why I haven't invested the time in the generation yet :P

saml-dev commented 6 months ago

here's what I mean by simple constants.go:

const (
    AppleTvAppAppleTv               = "com.apple.TVWatchList"
    AppleTvAppPlex                  = "com.plexapp.plex"
    AppleTvAppYoutube               = "com.google.ios.youtube"
    AppleTvAppYoutubeTv             = "com.google.ios.youtubeunplugged"
    AppleTvEntity                   = "media_player.apple_tv_living_room"
    AppleTvPaused                   = "paused"
    AppleTvPlaying                  = "playing"
    AqaraButtonDoublePress          = "double"
    AqaraButtonLongPress            = "hold"
    AqaraButtonLongRelease          = "release"
    AqaraButtonSinglePress          = "single"
    BackPatioLight                  = "switch.patio_light_door"
    BackYardLight                   = "switch.patio_light_yard"
    BasementMotion                  = "binary_sensor.basement_motion"
    BasementDoorLock                = "lock.basement_door"
// etc..
)
darrenparkinson commented 6 months ago

Thanks. So I was looking at the REST API to see how I might do this, but appears there isn't a simple /api/devices or /api/entities endpoint. The best I've been able to find is /api/template using:

{ "template": "{{ areas() }}" }

as the first request body, then, for each area, using (e.g.):

{ "template": "{{ area_entities('kitchen') }}" }

The content type for responses from /api/template are always text/plain; charset=utf-8 though (which could be converted I suppose).

Are you aware of any other options to get this -- took a brief look at the netdaemon code generation which looks more complicated than I'd need tbh. Any thoughts greatly appreciated.

I should mention I've only just started looking at home assistant, so not too familiar with the inner workings...

schmic commented 6 months ago

Thanks. So I was looking at the REST API to see how I might do this, but appears there isn't a simple /api/devices or /api/entities endpoint. The best I've been able to find is /api/template using:

{ "template": "{{ areas() }}" }

Just use the get_states api call and you will get all known entities with their properties.

https://developers.home-assistant.io/docs/api/websocket/#fetching-states