How to access pyscript.app_config in script

I’ve got a working pyscript script tied to an automation that processes items in a todo list and reads out a summary on the Voice satellite. In the interest of keeping things organized, I’ve got the .py file in a subfolder below pyscript/scripts. As I said, so far everything is working.

However, for organization and learning things I’ll want to know for more complex tasks, I want to put the id of the todo list in a config, as that’s the kind of thing that probably shouldn’t be hard coded.

However, I’m having a heck of a time figuring out how to do that. I’ve got a strong software development background, just not with python, so some of it’s quirks are a mystery to me.

I’ve followed the directions in the pyscript docs and put pyscript: !include pyscript/config.yaml in the master configuration.yaml, and in the referenced pyscript/config.yaml I’ve got the apps section with the name of the script and the setting I want to pass in.

However, within the script file, no matter what I do, it doesn’t recognize pyscript.app_config. The documentation states

Note that pyscript.app_config is not defined in regular scripts, only in each application’s main file.

But, not being super familiar with python, I have no idea what that means. The “Advanced Topics” section of the docs there is finally an example of accessing the app_config with…

for inst in pyscript.app_config:
    setup_triggers(**inst)

But that’s way more complicated than what I’m trying to do. I just want to set a string value in the config file to be used in the script.

Anybody know what the proper way to do this is? Thanks!

ETA, here’s my script, with the line I want to modify commented.

@service()
def summarizedWeatherAlerts():
  import json
  # I want to pull this value from the config  #########
  to_do_list_id = "todo.weather_alerts" 
  ######################################################
  summarized_alerts = []
  for alert in todo.get_items(entity_id=to_do_list_id, status=['needs_action'])[to_do_list_id]['items']:
    parsed_description = json.loads(alert["description"])
    summarized_alerts.append(f"{parsed_description['Event']}. {parsed_description['Headline'].lower()}. ")
  return { "text": "Next Alert. ".join(summarized_alerts) + " End of Weather Alerts" }

Are you planning to share this with other people? Otherwise you could just define a TodoManager, WeatherManager, or wherever you think makes sense to define the id and just use the value from there.

I’m trying to learn the right way to not hard code things that shouldn’t be hard coded and should be in a configuration, not just for this script but any more I create in the future, regardless of any intent to share them.

For consistency, they should all get said configuration in the same way, not sprinkled all over HA with different ways to set and fetch configurable variables for different scripts. pyscript docs indicate that this config.yaml file is the place to do that. I’m just trying to figure out how to read those values.

Im saying you can follow sound software architecture principles by defining the IDs where they make sense. A WeatherManager that handles all of the weather tasks should not necessarily need to leak the todo ID to other classes. Even when wanting to use it in automations, pyscripts seems to heavily rely on passing around functions as parameters and inner functions to create dynamic automations, so your LivingRoom class doesn’t ever actually need to know the ID, just use a function defined by your WeatherManager.

You might not be over optimizing, but even in the class handling the weather related services or automations, you would still only be changing one single variable if you defined the to_do_list_id as a variable of the class or the file instead of the function summarizedWeatherAlerts . The config is only needed if are planning on using it in multiple instances or as a library. Even if you want to do that in the future it should still be a change in a single place, but I think you might never actually need to.

I’m saying this because I don’t believe it is actually the right path to think of the pyscript as a collection of random scripts instead of as a full project that would greatly benefit from object oriented engineering (or functional programming if thats your thing). As you expand a collection of random functions is just going to be a mess no matter how clean of a config file you have.