custom-components / pyscript

Pyscript adds rich Python scripting to HASS
Apache License 2.0
892 stars 47 forks source link

Question: how to organize code in the case of private and shared apps? #134

Closed wsw70 closed 3 years ago

wsw70 commented 3 years ago

https://hacs-pyscript.readthedocs.io/en/stable/reference.html#configuration explains how to structure code and which directories are automatically loaded.

My usecase is the following (today everything is under <config>/pyscript/):

All the apps are independent, and only "grouped" together in these two groups.

The relevant configuration.yaml entries are

pyscript:
  allow_all_imports: true
  hass_is_global: true
  zigbee:
    light:
      salon_interrupteur_canape:
        single:
          - group.salon,off
          - group.salon_avant,toggle
        double:
          - group.salon,toggle
      salon_interrupteur_mur:
        single:
          - group.salon,toggle
        double:
          - group.michael,off
          - group.martin,off
          - group.parents,off
        hold:
          - group.salon_avant,toggle
     (...)
  rf433:
    light:
      9415E1:
        single:
          - switch.martin_entree,toggle
      9415E4:
        single:
          - switch.martin_pixar,toggle
      (...)
  meross:
    - name: meross01
      id: 2006176333738751801348e1e91fb91a
      channels: 1
      model: mss110
    - name: meross02
      id: 2005281078830290814148e1e91d807b
      channels: 6
    (...)

They are accessed by the apps via for instance pyscript.config['meross'] (a list in that case).

I keep a git repo for all my HA configuration, that includes pyscript.

I now have an extraordinary, groundbreaking app which I would like to share with the world :) - and this is an opportunity to redesign my directory structure.

First, my understanding is that the configuration variables discussed in the docs

pyscript:
  allow_all_imports: true
  apps:
     my_app1:
        # any settings for my_app1 go here
        hello: world
     my_app2:
        # any settings for my_app2 go here
        wazii: wazaa

are accessed as follows: pyscript.config['apps']['my_app1']['hello']. In other words, there is no link between the place a script is in the directories with its ability to reach for configuration variables (it cal get everything, even from other apps). This is OK, I just wanted to make sure.

So back to what I would like to have, I am divided between two approaches:

approach 1

genius_app is the one I would like to distribute, through a git repository in that directory that I would push to GitHub.

approach 2

Similar to the approach one, except that I would have all the apps in <config>/pyscript/apps/[dash|automation]/app_name/__init__.py

I have a hard time understanding the actual differences between these approaches, except that the second one has more boilerplate (and non-distinctive names for the apps (namely __init__.py))

Is this just a matter of simplifying the distribution? (in approach 2 all the apps are truly separated). What disturbs me in the second case is really the non-distinctive name.

EDIT: ah, I see that my solution nr 2 will not work, there can be only one subdirectory under apps. So no <config>/pyscript/apps/dash/someapp/__init__.py

wsw70 commented 3 years ago

I went for solution 1.

Any comments welcome if this was the wrong choice.

dlashua commented 3 years ago

The general idea is that "/scripts/" is for YOU, and "/apps/" is for things you've gotten from others or things you've written with the intent to reuse or share.

An "app" will not be loaded unless it has config in pyscript.apps.app_name_here (even if that config is empty). This is similar to Home Assistant in that, just because I have a custom_component code in my file system, that component is not loaded unless I have configuration (or via UI) saying to load it.

Also, apps can take on two naming conventions... 1) /apps/my_app_name/__init__.py: this is useful if your app is big enough to warrant having multiple files. Because you can then include /apps/my_app_name/someotherfile.py from inside of __init__.py. 2) /apps/my_app_name.py: this is useful if your app fits in a single file.

From what I can figure out by the naming you've used, "meross", "rf433", and "zigbee" are all likely separate apps. They are designed to be configured in YAML and perform certain tasks based on that configuration. Your "genius_app" is probably also an app. Not knowing enough about the "dash apps" I would guess these could go in scripts. The "automation apps" can probably also go in scripts.

You CAN, of course, use these features and directories any way you want. For me, "/scripts/" is used for things that wouldn't make any sense to share because they are specific automations that work in my home, and, while the overall design of the automation may be useful to others, they wouldn't be able to use the code in "/scripts/" as is, unless they happened to have rooms and sensors named exactly the same way I do. Anything that I have in "/apps/" is usable by anyone, since it's configured to your needs in YAML.

Put another way... "/apps/" is like a Home Assistant "custom_component". It probably needs to be configured and is useful to more than just you without modification. "/scripts/" is more like Home Assistant "automations", other people could use them, but not likely without modification. Home Assistant "scripts" can fall in either category depending on what it does.