shannonhochkins/ha-component-kit

Pull from HA and codgen types for all entities

Closed this issue · 10 comments

duxrat commented

Right now, entity names have to be passed manually. It works fine as long as you check the part you change, but those entities will change in HA over time, and the dashboard code will too.

I see the following problems that can arise out of this:

  • we won't be notified when referencing an entity that has changed in HA
  • just lacking the simple convenience of not having to look up entity names and double-check for typos
  • no guarantee of the entity domains types being up-to-date (for example, I got the error with the calendar domain, and those domains list can change over time
  • the same problems with entity attributes, which can get quite complex

I think the following would address those issues:

  • pull all entity names from HA
  • generate type with all of the names
  • add a script that polls and updates them continuously (every 1s, configurable, for example)
  • additional improvement (can be done later as it's way more complex) – do the same with attributes, codegen type for each entity and then use them in conditional type based on the string provided to the useEntity

Thanks for the feedback!

Entity types is something i will address, however this is something that would have to be polled by the user, i can easily create a script to sync & create types for a ha-instance, I did it initially for all the services/attributes i currently have, could be a simple cli script bundled with @hakit/core:

   hakit-core --sync-entities -user X -password - X

Leave that one with me, I'll try to sort, I myself haven't had any dramas with entities changing unless i have to re-add entities manually.

Another idea i had was to use the yaml configuration to list all the entities you want to use with the custom dashboard, and then retrieve them with the useHass hook const { getConfig } = useHass(); and use the entities from the config, that way you don't have to re-build / re-deploy anything you just have to update the yaml config from HA.

I realise it's not enough but i'm getting there! It's been a lot of work already to achieve what i have!

I'll see if i can come up with a nice solution here, I also need to provide error boundaries where the hooks try to use something that doesn't exist anymore

duxrat commented

Great, the work you've done so far made this flow possible, I appreciate it.
Unfortunately, I don't have enough time to work on this core, but I'll publish my dashboard on GH and later on forums when it's in a decent state to showcase the usage of this package and promote it.

Thanks a lot mate! I'll see what I can come up with so we have a tighter contract between the home assistant integration and react 🤘

I might ask that you explain your issue a little more @yep-dev if that's okay, I think I've figured out a way to basically "replace" the domains/services/entities that the package uses by default, however is that what you're after?

IE,

import { ServiceFunction, ServiceFunctionTypes, CUSTOM_SERVICES_KEY } from "@hakit/core";

declare module "@hakit/core" {
  export interface CustomServicesContainer<T extends ServiceFunctionTypes = "target"> {
    [CUSTOM_SERVICES_KEY]: { // <-- Use the unique symbol as a key
      light: {
        // Turn on one or more lights and adjust properties of the light, even when they are turned on already.
        turnOn: ServiceFunction<
          T,
          {
            color_name:'red';
          }
        >;
      };
    };
  }
}

This would basically only allow something like useEntity to validate with the light service, ie

const entity = useEntity('light.something');
entity.api.turnOn({
    color_name: 'red' // only accepts red now
});

This allows us to completely extend hakit with a predefined module that i can generate with a cli / node script which is married to the users home assistant instance.

Does this make sense? And if i've totally mis-interpreted your original question please let me know, We can do the same with entities to get intellisense but i wanted to drill down into your thoughts with the services first

If you want to give it a crack, I've released this sync tool!

https://shannonhochkins.github.io/ha-component-kit/?path=/docs/introduction-typescriptsync--docs

Obviously you'll need to update @hakit/core and also @hakit/components if you're using it!

@yep-dev have you managed to try it out yet?

duxrat commented

@shannonhochkins yes, I got some invalid output though, preparing the fix PR

duxrat commented

Works great; that services part is outstanding, saves a lot of code, and fiddling with HA dev tools, especially for people like me building all components from scratch.
Before:
2023-08-24 at 16 58 28@2x

After:
2023-08-24 at 17 00 09@2x

Awesome man! Glad you're liking it! I have a few more ideas to improve usability like automatic ways to tie in / notify failures of entity names that might change over time so I'll be focusing on that next

@yep-dev - i was looking through your repository (noticing a few things you're doing to get around bugs) - i noticed you're casting the api type for media_player, i hadn't realised but there was a bug with the api type helper, this has been fixed in the latest version :)