App that cycles a lights brightness between max and min while a button/switch is held down

Thanks guys, I really appreciate the help!

I was really struggling to get my head around the recursion aspect of it all!

Got it working like this:

import appdaemon.appapi as appapi

class Test(appapi.AppDaemon):
    def initialize(self):
        self.log("Test App Started.")

        self.going_up = True
        self.delay = 1
        self.minimum = 0
        self.maximum = 255
        self.step = 10
        self.switch_id = "input_boolean.rain_switch"
        self.light_id = "light.adams_study"
        self.listen_state(self.startfunc, self.switch_id)


    def startfunc(self, entity, attributes, old, new, kwargs):
        if new == "on":
            self.handle = self.run_in(self.runloop, self.delay)


    def runloop(self, kwargs):
        self.brightness = int(self.get_state(self.light_id, "brightness"))  # or float if that gives it back
        if self.going_up:
            self.brightness += self.step
            if self.brightness > self.maximum:
                self.brightness = self.maximum
                self.going_up = False
        else:
            self.brightness = self.brightness - self.step
            if self.brightness < self.minimum:
                self.brightness = self.minimum
                self.going_up = True

        self.turn_on(self.light_id, brightness=self.brightness)
        if self.check_for_button_state():
            self.run_in(self.runloop, self.delay)


    def check_for_button_state(self):
        if self.get_state(self.switch_id) == "on":
            return True
        else:
            return False