I have a lights.py
module that contains several apps for various light-related automations. As time has gone on, these apps contain lots of replicated functionality; for example, several apps contain the exact same logic to turn lights on at sunset:
import appdaemon.appapi as appapi
class LivingRoomLights(appapi.AppDaemon):
"""Define a class to represent the app."""
LIGHTS_SWITCH = 'group.living_room_lights'
def initialize(self):
"""Initialize."""
self.run_at_sunset(self.sunset_cb, offset=offset)
def sunset_cb(self, kwargs):
"""Turn on the living room lights just before sunset."""
self.turn_on(self.LIGHTS_SWITCH)
class MasterBedroomLights(appapi.AppDaemon):
"""Define a class to represent the app."""
LIGHTS_SWITCH = 'group.master_bedroom_lights'
def initialize(self):
"""Initialize."""
self.run_at_sunset(self.sunset_cb, offset=offset)
def sunset_cb(self, kwargs):
"""Turn on the living room lights just before sunset."""
self.turn_on(self.LIGHTS_SWITCH)
I’d appreciate some suggestions on how to extrapolate these common functionalities while retaining the individual apps – as an example, ideally, I’d love to have multiple lighting apps based on location, group, etc. and properly mix in functionality (like turning the lights on at sunset) as needed.
Thoughts? Thank you!