Part 4: Power DriversΒΆ

Power Drivers are used to control devices with binary states, either on or off. To create a new power driver from an empty template, run:

$ gardnr new power hello_world_power.py

There should now be a file called hello_world_power.py in your current directory. Open this file in your preferred code editor, it should contain:

from gardnr import drivers, logger


class Power(drivers.Power):
    """
    Add code to interface with physical powered devices.
    """

    def setup(self):
        """
        Add configuration here
        """

        # remove the next line and add code
        pass

    def on(self):

        """
        Communicate with a powered device

        Example:

        import subprocess
        command = 'gpio mode 0 out; gpio write 0 1'
        process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
        output, error = process.communicate()
        """

        # remove the next line and add code
        pass

    def off(self):
        pass

In the on method, remove the last two lines and insert:

print('Device turning on')

In the off method, remove the last line and insert:

print('Device turning off')

Your hello_world_power.py file should now look like:

from gardnr import drivers, logger


class Power(drivers.Power):
    """
    Add code to interface with physical powered devices.
    """

    def setup(self):
        """
        Add configuration here
        """

        # remove the next line and add code
        pass

    def on(self):

        """
        Communicate with a powered device

        Example:

        import subprocess
        command = 'gpio mode 0 out; gpio write 0 1'
        process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
        output, error = process.communicate()
        """

        print('Device turning on')

    def off(self):
        print('Device turning off')

Next, add the driver to GARDNR by running:

$ gardnr add driver hello-world-power power_driver:Power

To run the on method, run the following command:

$ gardnr power on hello-world-power

You should now see Device turning on displayed in the console. To run the off method, run the following command:

$ gardnr power off hello-world-power

You should now see Device turning off displayed in the console.

Like sensor and exporter drivers, power drivers can also be scheduled, which is described in Part 3: Scheduling. However, adding schedules for power drivers requires specifying the state as well. To put turning on a power driver on a schedule, run:

$ gardnr schedule add hello-world-power every-five-minutes on