Multiple class use in one file / entities configuration in apps.yaml

Hello,

I’m trying to write my apps with assumption to use one class methods… in different classes (in the same app file).

My general class:

class MyCheck(hass.Hass):
...
  # windows check
  def windows(self):
     self.log(self.args['windows'])
     for window in self.args['windows']:
       if self.get_state(window) == 'on':
         MyNotify.notify(self, "OPEN window LEFT: ", window)

  # doors check
  def doors(self):
     for door in self.args['doors']:
       if self.get_state(door) == 'on':
         MyNotify.notify(self, "OPEN door LEFT: ", door)
...

And I use class MyCheck in different classes:

class MyPresenceAwayMode(hass.Hass):
...

  def awaymodeswitch_on(self, entity, attribute, old, new, kwargs):
    MyCheck.windows(self)
    MyCheck.lamps(self)
...

class MyVacationMode(hass.Hass):
...

  def vacation_on(self, entity, attribute, old, new, kwargs):
    MyCheck.windows(self)
    MyCheck.doors(self)
    MyCheck.lamps(self)
...

I assumed that class MyCheck should include all my entities, so I tought my apps.yaml should like this:

my_presence_check:
  module: my_presence
  class: MyCheck
  windows:
     - binary_sensor.window_kr
     - binary_sensor.window_br
  doors:
     - binary_sensor.door_lr
     - binary_sensor.door_hw
my_presence_away:
  module: my_presence
  class: MyPresenceAwayMode
my_presence_vacation:
  module: my_presence
  class: MyVacationMode

but… in such an configuration it doesn’t work properly.

When I try to access MyCheck.windows(self) from class MyPresenceAwayMode I receive:
KeyError: 'windows'

So… it looks like that class MyCheck doesn’t have access to his ‘self’ configuration entities.

I found the way how to resolve situation:
I moved entities to particular MyPresenceAwayMode and MyVacationMode classes configuration:

my_presence_check:
  module: my_presence
  class: MyCheck
my_presence_away:
  module: my_presence
  class: MyPresenceAwayMode
  windows:
     - binary_sensor.window_kr
     - binary_sensor.window_br
  doors:
     - binary_sensor.door_lr
     - binary_sensor.door_hw
my_presence_vacation:
  module: my_presence
  class: MyVacationMode
  windows:
     - binary_sensor.window_kr
     - binary_sensor.window_br
  doors:
     - binary_sensor.door_lr
     - binary_sensor.door_hw

However… that’s not my final intention - I don’t want repeat my entities configuration for every particular class.

How can I access ‘self’ configuration entries for MyCheck class?

Greetings, M

you are going the wrong way.
Andrew created special functions for that.

the Mycheck class should be in the yaml like you have it:

my_presence_check:
  module: my_presence
  class: MyCheck
  windows:
     - binary_sensor.window_kr
     - binary_sensor.window_br
  doors:
     - binary_sensor.door_lr
     - binary_sensor.door_hw

in the next class you then can use the function get_app like this:

class MyPresenceAwayMode(hass.Hass):
...

  def awaymodeswitch_on(self, entity, attribute, old, new, kwargs):
    MyCheck = self.get_app("my_presence_check")
    MyCheck.windows()
    MyCheck.lamps()

and that can go in the yaml like this:

my_presence_away:
  module: my_presence
  class: MyPresenceAwayMode
  dependencies:
    - my_presence_check

you dont even have to put the classes in the same app, you can just split it all up into several py scripts and also the yaml can be in several files for each app.

It works. Thank you :wink:
I rewrote my other apps as well.

Greetings, M

1 Like