AppDaemon -> secrets.yaml

Hello,

How can I access values from secrets.yaml and use in AppDaemon apps?

get_hass_config doesn’t return such detailed data.

Regards,
M

You just specify the secrets file you want to use in the configuration file http://appdaemon.readthedocs.io/en/latest/INSTALL.html?highlight=secrets#secrets

secrets.yaml is used to use secrets in your configuration (appdaemon.yaml) and not in apps.

I have the same question about secrets. How can I access the secrets.yaml in my code, as follows?

class send_email(hass.Hass):
    def initialize(self):
        self.log('initializing send email')
        self.listen_state(self.check_status, "binary_sensor.garage_workshop_sensor_sensor")
    def check_status(self, entity, attribute, old, new, kwargs):
        self.log('sending email')
        password = !secret gmail_password
        # creates SMTP session 
        s = smtplib.SMTP('smtp.gmail.com', 587) 
        if new == "on":
            message = "garage door is open"
        elif new == "off":
            message = "garage door is shut"
        s.starttls()
        s.login("[email protected]", password)
        s.set_debuglevel(1)        
        s.sendmail("[email protected]", "[email protected]", message)
        self.log(message)
        s.quit()
File "<frozen importlib._bootstrap_external>", line 669, in exec_module

File “”, line 775, in get_code
File “”, line 735, in source_to_code
File “”, line 222, in _call_with_frames_removed
File “/home/homeassistant/conf/apps/test.py”, line 13
password = !secret gmail_password
^
SyntaxError: invalid syntax

you cant

but in your case i would setup the smpt notification in HA
and use notify in the app.
then you dont need a PW in the app.

Thanks, I will try that approach!

1 Like

Rene, it works as advertised. I was hoping to use an appdaemon app, however. The appdaemon app works, but exposes the gmail password :frowning:

as long as you think that HA is what you choose, i think its wise to keep as much in HA as possible.

i keep everything remotely looking as an entity in HA.
everything that is an automation is in AD.

unless you can do something with a component in AD that you cant do in HA, then of course i also use AD.

Hi,
Here are my 2 cents about how I use the HA secrets values in AD. It is a solution per secret value.

on secrets.yaml I have:

my_secret: 123456789ABCDEF

I have an AD util app that holds various methods I use on my other apps.
A method related to the secret can be:

    def get_my_secret(self):
        return self.args["my_secret"]

on AD apps.yaml I define the utils app with an extra parameter like:

utils:
  module: utils
  class: utils
  my_secret: !secret my_secret

(consider using dependencies on the actual app)

and this is the AD app I want to use the secret with:

  def initialize(self):
    self.utils = self.get_app('utils')   
    .
    .
    .
    secret = self.utils.get_my_secret()

You could of course skip the utils app and add the parameter directly to the app, but if you need the secret in more than one app, you will end up duplicating your code.
If you have several secrets with similar names but different suffix (secret_a, secret_b, etc.), you can use parameter in the utils function…

5 Likes

Just don’t forget to restart Appdaemon after creating the new entry in secrets.yaml!
It seems this file is cached at start.