Extending MudPi

MudPi was designed to be highly configurable in order to meet a variety of automation setups. The entire system was designed to be expanded and extended easily with new components. While a large number of components are already supported, you may have a new device not added yet that you want to add. Below is an example of how to extend MudPi with a new sensor.

Adding a New Sensor

To begin extending MudPi with a new sensor you first need to decide on an appropriate type for this sensor to have. This choice will be reflected throughout this process. In our example we are going to add a new Motion type sensor that takes a simple digital reading.

When loading in sensor configurations MudPi uses the sensor type in order to dynamically import the proper class for the sensor. For example if we want to add a new Motion type sensor then we will need to create a file motion_sensor.py in the /sensors/pi folder that contains a child class of Sensor called MotionSensor. You can learn more about how workers operate under the workers section.

The Sensor Class

The base sensor class has two important methods init_sensor() and read(). You will need to override both of these methods with the appropriate actions for initializing and reading data from the sensor.

The init_sensor() method is where you will take care of any preparations to get the component ready. This method will only be ran once on load.

The read() method is called by the sensor worker on each loop and expects to get data back from the sensor. The return value can be an individual value (such as an int or float) or dict of values.

For our Motion type sensor below is an example of how we might add in our new motion_sensor.py file containing the MotionSensor class. This file would be stored under /sensors/pi/motion_sensor.py. Keep in mind this file is where you would actually import any libraries needed and use them to interact with the sensor/component. For the sake of this tutorial this class is just doing a simple digital read.

from .sensor import Sensor
import RPi.GPIO as GPIO

class MotionSensor(Sensor):

	def __init__(self, pin, name=None, key=None, redis_conn=None):
		super().__init__(pin, name=name, key=key, redis_conn=redis_conn)
		return

	def init_sensor(self):
		GPIO.setup(self.pin, GPIO.IN)
		return

	def read(self):
		# Motion Detected!
		value = GPIO.input(self.pin)
		return value

Loading The New Sensor

Support for our new sensor is now completed! This sensor is able to be controlled by the worker and start gathering data. In order to load our new sensor we will need to provide a configuration for it. The configuration below will create a new Motion sensor for us connected to pin 8 with a key of my_motion_sensor.

{
    "workers":[{
        "type":"sensor",
        "sensors": [
            {
                "type":"Motion",
                "pin": 8,
                "key":"my_motion_sensor"
            }
        ]
    }]
}

Submit a Pull Request

Once you added support for your new sensor and have a chance to test it then you may want to consider posting your updates for others to use. This can be done by submitting a pull request to the main mudpi/mudpi-core with your updates. If you enjoy using MudPi this is a great way to contribute to the project and help it grow. Also donating to my efforts is another huge help!