Sign inSign up

johnthenerd/lightapi

By johnthenerd

Updated over 4 years ago

https://github.com/JohnTheNerd/lightAPI

Image
0

10K+

johnthenerd/lightapi repository overview

lightAPI

A simplistic smart lighting application written in Python 3. It has a Flask-based API to control all light functionality. I run it on my little Raspberry Pi to manage my lights (and popcorn maker). It currently only works with the HS100 smartplug and LifX/Yeelight bulbs. It is also possible to implement all the functions in devices/yeelight.py for any device, which will make the API automatically able to communicate to the bulb by setting the filename as brand in the configuration file.

Everything in the API works as GET requests - this is an intentional design decision. This allows one to trigger any function in the API straight from the browser!

Usage

  • Clone this repository.

  • Install all the requirements by running pip3 install pyHS100 flask lifxlan.

  • Rename config.dist.json to config.json.

  • Open config.json in any text editor and change variables as necessary. All times are in seconds.

    • Lights: Define groups of lights here. multizone represents a LifX Z, and mac/ip should be self-explanatory.
    • Colors: All colors are lists of four numbers, as can be seen in the configuration.
    • Passwords: The passwords needed for certain API endpoints. All passwords are SHA-512 hashes of the actual password
    • Alarm: initialFadeIn is the amount of time cyan light initially fades in, duration is the total duration of the alarm, and colorChangeFrequency is how often the color will rotate after the initial fade-in. You could install ffplay and put a file under the alarm directory named alarm.mp3 if you'd like to hear an alarm too. If you don't have any form of sensitivity to flashing shiny lights and just need to get out of bed, you could change the mode to insane. If you do so, you must also:
      • Define colors as a list of colors you'd like to use.
      • You may define slack to automatically post to a Slack channel a certain period after the alarm starts, if it's still running of course. If you do so, you must also define all the options you can find under config.dist.json.
      • If running on Docker, define hostIP as the IP address of the computer this is running on.
    • Camera: E-mail options for the camera setting.
    • Plug: Optional field, if not used must be entirely removed. ip is the IP address of your HS100 smart plug, offTimer is an optional timer set for the on command, and onRoutes/offRoutes are the additional endpoints used to control the plug.
    • skipGroups: A list of groups which are to be skipped when looking for all lights.
    • myIP: Must correspond to your local IP address. 0.0.0.0 may work if you want to have it wide open, 127.0.0.1 will lock it down to your local computer
    • hosts: Optional field, must be an array of strings if used. If the "Host" header does not match one of the hosts provided here (don't include http:// or the port), the connection will be rejected. Useful against DNS rebinding attacks.
  • Run api/main.py. There is also an example lightAPI.service file you can copy to /etc/systemd/system/ (after changing the paths) and run sudo systemctl enable lightAPI to have the API continuously run for you!

  • If you'd like to have alarm support, run alarm/main.py. The only endpoints are /set/<year>/<month>/<day>/<hour>/<minute>/<second> and /stop/<password> and settings are read as documented above.

  • To use any module, you can either call the corresponding API endpoints through a browser or just run main.py under their folder.

  • In order to use the Tasker profiles, you must have purchased all of Tasker, AutoWear, and AutoAlarm. Afterwards,

    • Import all tasks inside the tasker/tasks directory.
    • Import all profiles inside the tasker/profiles directory.
    • Open each task and change raspberrypi to the IP address of your Raspberry Pi.

Oh, it also runs on Docker. You could create a config.json file and run:

docker build -t lightapi .
docker run -p 5000:5000 lightapi    # add "--restart always" to continuously restart and "-p 54433:54433" if you have a yeelight bulb

Endpoints

  • http://my.ip.goes.here:5000/camera/<light>

    Triggers a camera alarm, making the light full brightness on white color and sending an e-mail if configured. Your camera system should be pointing here.

    • light: The light to turn on.
  • http://my.ip.goes.here:5000/camera/off/<password>

    Disarms the camera module. If the given password is incorrect, returns with a naive 404.

    • password: The password needed, before it's SHA512-hashed.
  • http://my.ip.goes.here:5000/camera/on

    Arms the camera module.

  • http://my.ip.goes.here:5000/colors

    Starts color rotation, for all lights.

  • http://my.ip.goes.here:5000/colors/<seconds>

    Starts color rotation for all lights, with a pre-defined number of seconds between full color changes.

    • seconds: The number of seconds between color changes.
  • http://my.ip.goes.here:5000/rotate/stop

    Stops all color rotation, for all lights.

  • http://my.ip.goes.here:5000/

    Shows the status of all devices.

  • http://my.ip.goes.here:5000/<light>/set/<hue>/<saturation>/<brightness>/<kelvin>

    You can use this to manually set a light color. You can take the values returned from the main page and put them here.

    • light: The light to be affected.
    • hue: The hue value for the light, between 1-65535.
    • saturation: The saturation value for the light, between 1-65535.
    • brightness: The brightness value for the light, between 1-65535.
    • kelvin: The kelvin value for the light, betweem 2500-9000.
  • http://my.ip.goes.here:5000/<light>/off/<seconds>

    A simple timer. The light will slowly dim down and turn off afterwards. Keep in mind that I intentionally add 5% to the given value because of some race conditions.

    • light: The light to be affected.
    • seconds: The number of seconds after which the light will shut off.
  • http://my.ip.goes.here:5000/<light>/random

    Sets the light to a random color.

    • light: The light to be affected.
  • http://my.ip.goes.here:5000/<light>/random/<brightness>

    Sets the light to a random color, with a provided brightness.

    • light: The light to be affected.
    • brightness: The brightness requested, in percent.
  • http://my.ip.goes.here:5000/<light>/blink

    Blinks the light three times, rapidly dimming and re-brightening the light over the span of a second or two.

    • light: The light to be affected.
  • http://my.ip.goes.here:5000/<light>/blink/<count>

    Blinks the light a given number of times, rapidly dimming and re-brightening the light over the span of a second or two.

    • light: The light to be affected.
    • count: The number of times to blink the light.
  • http://my.ip.goes.here:5000/<light>/blink/<count>/<brightness>

    Blinks the light a given number of times, rapidly dimming and re-brightening the light over the span of a second or two. Also allows you to specify the brightness level to dim it to.

    • light: The light to be affected.
    • count: The number of times to blink the light.
    • brightness: The brightness to dim down to, in percent.
  • http://my.ip.goes.here:5000/<light>/blink/<color>/<count>/<brightness>

    Blinks the light in a specific color, a given number of times. If multiple lights are present in a group, they will all blink sequentially.

    • light: The light to be affected.
    • color: The color to blink it in.
    • count: The number of times to blink it.
    • brightness: The brightness value, in percent.
  • http://my.ip.goes.here:5000/<light>/<color>

    Sets a light to a specific color.

    • light: The light to be affected.
    • color: The color to set it to.
  • http://my.ip.goes.here:5000/<light>/<color>/<brightness>

    Sets a light to a specific color with a specific brightness level.

    • light: The light to be affected.
    • color: The color to set it to.
    • brightness: The brightness level in percent.
  • http://my.ip.goes.here:5000/<light>/<color>/<brightness>/<seconds>/<otherBrightness>

    Sets a light to a specific color with a specific brightness level, dimming or brightening it over time.

    • light: The light to be affected.
    • color: The color to set it to.
    • brightness: The brightness level in percent.
    • seconds: The number of seconds to change the brightness over.
    • otherBrightness: The next brightness level in percent.
  • http://my.ip.goes.here:5000/<light>/on

    Turns the light on.

    • light: The light to turn on.
  • http://my.ip.goes.here:5000/<light>/brightness/<brightness>

    Sets a light to a specific brightness. You can also use + or - before your number to increase or decrease the brightness by that amount. For example, setting brightness to +10 will brighten the light by 10%.

    • light: The light to be affected.
    • brightness: The brightness value in percent.
  • http://my.ip.goes.here:5000/<light>/off

    Turns the light off.

    • light: The light to turn off.
  • http://my.ip.goes.here:5000/<light>/toggle

    Toggles the light.

    • light: The light to toggle.
  • http://my.ip.goes.here:5000/toggle

    Toggles all lights. If a majority of the lights are on, it will turn all lights off, and vice versa.

  • http://my.ip.goes.here:5000/toggle/<color>

    Toggles all lights. If a majority of the lights are on, it will turn all lights off, and vice versa.

    • color: The color to set lights to, if turning them on
  • http://my.ip.goes.here:5000/<light>/toggle/<color>

    Toggles the light.

    • light: The light to toggle.
    • color: The color to change it to
  • http://my.ip.goes.here:5000/<light>/rotate/<seconds>

    Starts fading the light to different colors.

    • light: The light to be affected.
    • seconds: The number of seconds between full color changes.
  • http://my.ip.goes.here:5000/<light>/rotate/stop

    Stops color rotation for a given light.

    • light: The light to be affected.

Tag summary

Content type

Image

Digest

Size

497.9 MB

Last updated

over 4 years ago

docker pull johnthenerd/lightapi