A versatile and extensible platform automation, with hundreds of supported integrations
Find a file
2021-02-24 22:41:16 +01:00
bin s/github.com/git.platypush.tech/g 2021-01-23 15:03:43 +01:00
docs Removed docs references to removed plugin media.ctrl 2021-02-21 23:34:47 +01:00
examples Updated README (it hadn't been updated for ages) 2021-02-22 02:53:20 +01:00
platypush The /hook route should not require token authentication - authentication is up to the hook implementation 2021-02-24 22:21:01 +01:00
tests .context needs to be imported in test_event_parse to make sure that the configuration is properly initialized 2021-02-24 00:35:41 +01:00
.gitignore Added new static files 2020-11-26 12:41:56 +01:00
.gitmodules No need to keep the wiki as a submodule 2021-02-02 01:26:54 +01:00
.readthedocs.yml Minimum required Python version bumped (3.5->3.6) and readthedocs Python interpreter upgraded (3.6->3.7). 2020-08-22 15:28:52 +02:00
.travis.requirements pyjwt added to Travis-CI requirements list 2021-02-21 18:57:15 +01:00
.travis.yml Got rid of Python 3.6 test suite 2019-07-29 11:30:07 +02:00
Dockerfile Refactored platydock and platyvenv 2019-12-01 22:27:54 +01:00
generate_missing_docs.py Added documentation for response objects as well 2020-03-05 17:41:26 +01:00
LICENSE.txt Copyright year update 2020-09-28 13:11:07 +02:00
MANIFEST.in The webapp dist/ folder should actually be under the webapp dir 2021-02-20 23:26:25 +01:00
README.md Added README note on how to run tests 2021-02-24 22:40:39 +01:00
requirements.txt Added support for JWT token-based authentication 2021-02-12 22:43:34 +01:00
run_tests.sh Improved tests script 2021-02-23 01:09:03 +01:00
setup.cfg Bump version: 0.13.9 → 0.20.0 2021-02-24 22:41:16 +01:00
setup.py Bump version: 0.13.9 → 0.20.0 2021-02-24 22:41:16 +01:00

Platypush

Build Status Documentation Status pip version License Last Commit Contributions

  • Recommended read: Getting started with Platypush.

  • The blog is in general a good place to get more insights on what you can build with it and inspiration about possible usages.

  • The wiki also contains many resources on getting started.

  • Extensive documentation for all the available integrations and messages is available on ReadTheDocs.

  • If you have issues/feature requests/enhancement ideas please create an issue.

  • A Reddit channel is also available for more general questions.


Platypush is a general-purpose extensible platform for automation and integration across multiple services and devices.

It enables users to create their own self-hosted pieces of automation based on events (if this happens then do that) and it provides a comprehensive and customizable user interface that collects everything you need to visualize and control under one roof.

It takes some concepts from IFTTT, Tasker, Microsoft Flow, PushBullet and Home Assistant to provide an environment where the user can easily connect things together.

Its ideal home is a single-board computer like a RaspberryPi that you can configure to orchestrate any home automation and cloud automation in your own living room or garage, but it can easily run on any device that can run a Python interpreter, and the bar for the hardware requirements is very low as well - I use it to run pieces of automation on devices as powerful as a RaspberryPi Zero or an old Nokia N900 with Linux.

You can use Platypush to do things like:

Architecture

The architecture of Platypush consists of a few simple pieces, orchestrated by a configuration file stored by default under ~/.config/platypush/config.yaml:

Plugins

They are integrations that do things - like modify files, train and evaluate machine learning models, control cameras, read sensors, parse a web page, control lights, send emails, control Chromecasts, run voice queries, handle torrent transfers or control Zigbee or Z-Wave devices.

The configuration of a plugin matches one-on-one that of its documented class constructor, so it's very straightforward to write a configuration for a plugin by reading its documentation:

light.hue:
  # Groups that will be controlled by default
  groups:
    - Living Room
    - Hall

Actions

Plugins expose actions, that match one-on-one the plugin class methods denoted by @action, so it's very straightforward to invoke plugin actions by just reading the plugin documentation. They can be invoked directly from your own scripts or they can be sent to the platform through any supported channel as simple JSON messages:

{
  "type": "request",
  "action": "light.hue.on",
  "args": {
    "lights": ["Entrance Bulb"]
  }
}

Backends

They are background services that either listen for messages on channels (like an HTTP backend, an MQTT instance, a Kafka instance, a Websocket service, Pushbullet etc.) or monitor a device or a service for events (like a sensor, a custom voice assistant, a bridge running on a Zigbee or Z-Wave, an NFC card reader, a MIDI device, a Telegram channel, a Bluetooth scanner etc.).

If a backend supports the execution of requests (e.g. HTTP, MQTT, Kafka, Websocket and TCP) then you can send requests to these services in JSON format. For example, in the case of the HTTP backend:

# Get a token
curl -XPOST -H 'Content-Type: application/json' -d '
  {
    "username": "$YOUR_USER",
    "password": "$YOUR_PASSWORD"
  }' http://host:8008/auth

# Execute a request

curl -XPOST -H 'Content-Type: application/json' -H "Authorization: Bearer $YOUR_TOKEN" -d '
  {
    "type": "request",
    "action": "tts.say",
    "args": {
      "text": "This is a test"
    }
  }' http://host:8008/execute

Events

When a certain event occurs (e.g. a JSON request is received, or a Bluetooth device is connected, or a Flic button is pressed, or some speech is detected on the voice assistant service, or an RSS feed has new items, or a new email is received, or a new track is played, or an NFC tag is detected, or new sensor data is available, or a value of a Zigbee device changes, etc.), the associated backend will trigger an event.

Hooks

Event hooks are custom pieces of logic that will be run when a certain event is triggered. Hooks are the glue that connects events to actions, exposing a paradigm similar to IFTTT (if a certain event happens then run these actions). They can declared as:

event.hook.SearchSongVoiceCommand:
  if:
    type: platypush.message.event.assistant.SpeechRecognizedEvent
    phrase: "play ${title} by ${artist}"
  then:
    - action: music.mpd.clear
    - action: music.mpd.search
      args:
        filter:
          artist: ${artist}
          title: ${title}

    - if ${len(output)}:
      - action: music.mpd.play
        args:
          resource: ${output[0]['file']}
  • Stand-alone Python scripts stored under ~/.config/platypush/scripts and will be dynamically imported at start time. Example:
from platypush.event.hook import hook
from platypush.utils import run
from platypush.message.event.assistant import SpeechRecognizedEvent

@hook(SpeechRecognizedEvent, phrase='play ${title} by ${artist}')
def on_music_play_command(event, title=None, artist=None, **context):
  results = run('music.mpd.search', filter={
    'artist': artist,
    'title': title,
  })

  if results:
    run('music.mpd.play', results[0]['file'])

Procedures

Procedures are pieces of custom logic that can be executed as atomic actions using procedure.<name> as an action name. They can be defined either in the config.yaml or as Python scripts stored under ~/.config/platypush/scripts - provided that the procedure is also imported in ~/.config/platypush/scripts/__init__.py so it can be discovered by the service.

YAML example for a procedure that can be executed when we arrive home and turns on the lights if the luminosity is lower that a certain thresholds, says a welcome home message using the TTS engine and starts playing the music:

procedure.at_home:
    # Get luminosity data from a sensor - e.g. LTR559
    - action: gpio.sensor.ltr559.get_data

    # If it's lower than a certain threshold, turn on the lights
    - if ${int(light or 0) < 110}:
        - action: light.hue.on

    # Say a welcome home message
    - action: tts.google.say
      args:
        text: Welcome home

    # Play the music
    - action: music.mpd.play

Python example:

# Content of ~/.config/platypush/scripts/home.py
from platypush.procedure import procedure
from platypush.utils import run

@procedure
def at_home(**context):
  sensor_data = run('gpio.sensor.ltr559.get_data')
  if sensor_data['light'] < 110:
    run('light.hue.on')

  run('tts.google.say', text='Welcome home')
  run('music.mpd.play')

In either case, you can easily trigger the at-home procedure by sending an action request message to a backend - for example, over the HTTP backend:

curl -XPOST -H 'Content-Type: application/json' -H "Authorization: Bearer $YOUR_TOKEN" -d '
  {
    "type": "request",
    "action": "procedure.at_home"
  }' http://host:8008/execute

The web interface

If backend.http is enabled then a web interface will be provided by default on http://host:8008/. Besides using the /execute endpoint for running requests, the built-in web server also provides a full-featured interface that groups together the controls for most of the plugins - e.g. sensors, switches, music controls and search, media library and torrent management, lights, Zigbee/Z-Wave devices and so on. The UI is responsive and mobile-friendly.

The web service also provides means for the user to create custom dashboards that can be used to show information from multiple sources on a large screen.

Installation

System installation

Platypush uses Redis to deliver and store requests and temporary messages:

# Example for Debian-based distributions
[sudo] apt-get install redis-server

# Enable and start the service
[sudo] systemctl enable redis
[sudo] systemctl start redis

To install the core platform:

  • The pip way:
[sudo] pip3 install platypush
  • The sources way:
git clone https://git.platypush.tech/platypush/platypush.git
cd platypush
[sudo] python3 setup.py install

Then install the extensions that you wish to use. There are a few ways to check the dependencies required by an extension:

Check their extras name in extras_require under setup.py.

If you follow this route then you can install the extra dependencies in one of the following ways:

  1. pip installation:
[sudo] pip3 install 'platypush[extra1,extra2,extra3]'
  1. Sources installation:
cd $DIR_TO_PLATYPUSH
[sudo] pip3 install '.[extra1,extra2,extra3]'

Check the dependencies/installation instructions reported under the plugin/backend documentation.

If you follow this route then simply run the commands listed in the plugin/backend documentation to get the dependencies installed.

Check/uncomment the associated lines in requirements.txt.

If you follow this route then uncomment the lines in requirements.txt associated to the plugins/backends that you want to use and run:

[sudo] pip3 install -r requirements.txt

After installing the dependencies, create a configuration file under ~/.config/platypush/config.yaml (the application can load the configuration from another location through the -c option) containing the configuration of the backend and plugins that you want to use, and add any hooks and procedures for your use case.

You can then start the service by simply running:

platypush

It's advised to run it as a systemd service though - simply copy the provided .service file to ~/.config/systemd/user, check if the path of platypush matches the path where it's installed on your system, and start the service via systemctl:

systemctl --user start platypush

Virtual environment installation

Platypush provides a script named platyvenv that can parse a config.yaml and automatically create a virtual environment (under ~/.local/share/platypush/venv/<device_id>) with all the dependencies required by the configured integrations.

  1. Create the environment from a configuration file:
platyvenv build -c /path/to/config.yaml
  1. Start the service from the virtual environment:
# device_id matches either the hostname or the device_id in config.yaml
platyvenv start device_id
  1. Stop the instance:
platyvenv stop device_id
  1. Remove the instance:
platyvenv rm device_id

Docker installation

You can also install Platypush in a container - the application provides a script named platydock that automatically creates a container instance from a config.yaml:

  1. Create the container from a configuration file:
platydock build -c /path/to/config.yaml
  1. Start the container:
# device_id matches either the hostname or the device_id in config.yaml
platydock start device_id
  1. Stop the instance:
platydock stop device_id
  1. Remove the instance:
platyvdock rm device_id

Tests

To run the tests simply run pytest from the source root folder - no further configuration required.


Funding

If you use and love Platypush, please consider buying me a coffee/beer.

I've been working on Platypush all by myself in my spare time for the past few years, and I've made sure that it remains open and free.

If you like this product, please consider supporting - I'm definitely not planning to get rich with this project, but I'd love to have at least the costs for the server covered by users.

Issues and requests opened by donors will also be given priority over others.