Supereg / homebridge-http-lightbulb

Powerful http lightbulb for Homebridge: https://github.com/homebridge/homebridge
ISC License
23 stars 7 forks source link
hap homebridge homebridge-http homebridge-plugin homekit http http-lighbulb http-light javascript light lightbulb mqtt mqtt-smarthome notifications regex

homebridge-http-lightbulb Plugin

npm npm GitHub Workflow Status GitHub issues GitHub pull requests

homebridge-http-lightbulb is a Homebridge plugin with which you can configure HomeKit light bulbs which forward any requests to a defined http server. This comes in handy when you already have home automated equipment which can be controlled via http requests. Or you have built your own equipment, for example some sort of lightning controlled with an wifi enabled Arduino board which than can be integrated via this plugin into Homebridge.

Installation

First of all you need to have Homebridge installed. Refer to the repo for instructions.
Then run the following command to install homebridge-http-lightbulb

sudo npm install -g homebridge-http-lightbulb

Updating the light bulb state in HomeKit

All characteristic from the 'lightbulb' service have the permission to notify the HomeKit controller of state changes. homebridge-http-lightbulb supports two concepts to send state changes to HomeKit.

The 'pull' way:

The 'pull' way is probably the easiest to set up and supported in every scenario. homebridge-http-lightbulb requests the state of the light in an specified interval (pulling) and sends the value to HomeKit. However the pull way is currently only supported for the 'On' characteristic!
Look for pullInterval in the list of configuration options if you want to configure it.

The 'push' way:

When using the 'push' concept, the http device itself sends the updated value to homebridge-http-lightbulb whenever values change. This is more efficient as the new value is updated instantly and homebridge-http-lightbulb does not need to make needless requests when the value didn't actually change.
However because the http device needs to actively notify the homebridge-http-lightbulb there is more work needed to implement this method into your http device.

Using MQTT:

MQTT (Message Queuing Telemetry Transport) is a protocol widely used by IoT devices. IoT devices can publish messages on a certain topic to the MQTT broker which then sends this message to all clients subscribed to the specified topic. In order to use MQTT you need to setup a broker server (mosquitto is a solid open source MQTT broker running perfectly on a device like the Raspberry Pi) and then instruct all clients to publish/subscribe to it.

Using 'homebridge-http-notification-server':

For those of you who are developing the http device by themselves I developed a pretty simple 'protocol' based on http to send push-updates.
How to implement the protocol into your http device can be read in the chapter Notification Server

Configuration:

The configuration can contain the following properties:

Basic configuration options:
Advanced configuration options:

In the Examples section are some example configurations to get you started.

Experimental configuration options

The options in this section are all part of some experimental features and can change in any update.

I'm currently experimenting with allowing setting and querying device status with mqtt alongside http. So that you are for example able to manage the ON characteristic fully over mqtt and the color and brightness values over http. Currently this is only supported for the ON characteristic but I'm planning to add this for any combination of characteristics and in the long run also to get added into my homebridge-http-switch plugin if everything works well.

Placeholders in setUrl properties

On every set there are the following placeholders available which will be replaced with the respective value.
Note that for example when the setUrl for the brightness characteristic is called, %s will be replaced with the new value and %brightness will be replaced with the current/old value.
The value for the placeholders will be supplied in the specified unit.

UrlObject

A urlObject can have the following properties:

Below is an example of an urlObject containing all properties:

{
  "url": "http://example.com:8080",
  "method": "GET",
  "body": "exampleBody",

  "strictSSL": false,

  "auth": {
    "username": "yourUsername",
    "password": "yourPassword"
  },

  "headers": {
    "Content-Type": "text/html"
  }
}

MQTTObject

A mqttObject can have the following properties:

Basic configuration options:
Advanced configuration options:

Below is an example of an mqttObject containing the basic properties for a light bulb service:

{
  "host": "127.0.0.1",
  "port": "1883",

  "credentials": {
    "username": "yourUsername",
    "password": "yourPassword"
  },

  "subscriptions": [
    {
      "topic": "your/topic/here",
      "characteristic": "On",
      "messagePattern": "on"
    },
    {
      "topic": "your/other/topic/here",
      "characteristic": "Brightens",
      "messagePattern": "([0-9]{1,3})"
    }
  ]
}

Examples

Basic light bulb with power and brightness

This is a basic light bulb configuration supporting the required On and the optional brightness characteristic.
Note that every url is simply a string and are only examples. You could also define every url using a urlObject.

{
  "accessory": "HTTP-LIGHTBULB",
  "name": "Light",

  "onUrl": "http://localhost/api/lightOn",
  "offUrl": "http://localhost/api/lightOff",
  "statusUrl": "http://localhost/api/lightStatus",

  "brightness": {
    "setUrl": "http://localhost/api/setBrightness?brightness=%s",
    "statusUrl": "http://localhost/api/getBrightness"
  }
}

Light bulb supporting color

{
  "accessory": "HTTP-LIGHTBULB",
  "name": "Light",

  "onUrl": "http://localhost/api/lightOn",
  "offUrl": "http://localhost/api/lightOff",
  "statusUrl": "http://localhost/api/lightStatus",

  "brightness": {
    "setUrl": "http://localhost/api/setBrightness?brightness=%s",
    "statusUrl": "http://localhost/api/getBrightness"
  },

  "hue": {
    "setUrl": "http://localhost/api/setHue?hue=%s",
    "statusUrl": "http://localhost/api/getHue"
  },
  "saturation": {
    "setUrl": "http://localhost/api/setSaturation?saturation=%s",
    "statusUrl": "http://localhost/api/getSaturation"
  }
}

Light bulb support color temperature

{
  "accessory": "HTTP-LIGHTBULB",
  "name": "Light",

  "onUrl": "http://localhost/api/lightOn",
  "offUrl": "http://localhost/api/lightOff",
  "statusUrl": "http://localhost/api/lightStatus",

  "brightness": {
    "setUrl": "http://localhost/api/setBrightness?brightness=%s",
    "statusUrl": "http://localhost/api/getBrightness"
  },

  "colorTemperature": {
    "setUrl": "http://localhost/api/setColorTemperature?temperature=%s",
    "statusUrl": "http://localhost/api/getColorTemperature",
    "unit": "mired"
  }
}

Light bulb using body parameters

{
  "accessory": "HTTP-LIGHTBULB",
  "name": "Light",
  "debug": true,
  "onUrl": {
    "url": "http://localhost/api/light",
    "method": "PUT",
    "body": "{ \"on\": 1 }"
  },
  "offUrl": {
    "url": "http://localhost/api/light",
    "method": "PUT",
    "body": "{ \"on\": 0 }"
  },
  "statusUrl": "http://localhost/api/light",
  "brightness": {
    "statusUrl": "http://localhost/api/light",
    "setUrl": {
      "url": "http://localhost/api/light",
      "method": "PUT",
      "body": "{ \"brightness\": %s }"
    }
  },
  "colorTemperature": {
    "statusUrl": "http://localhost/api/light",
    "unit": "mired",
    "minValue": 143,
    "maxValue": 344,
    "setUrl": {
      "url": "http://localhost/api/light",
      "method": "PUT",
      "body": "{\"temperature\": %s }"
    }
  }
}

Notification Server

homebridge-http-lightbulb can be used together with homebridge-http-notification-server in order to receive updates when the state changes at your external program. For details on how to implement those updates and how to install and configure homebridge-http-notification-server, please refer to the README of the repository first.

Down here is an example on how to configure homebridge-http-lightbulb to work with your implementation of the homebridge-http-notification-server.

{
    "accessories": [
        {
          "accessory": "HTTP-LIGHTBULB",
          "name": "Light",

          "notificationID": "my-switlightch",
          "notificationPassword": "superSecretPassword",

          "onUrl": "http://localhost/api/lightOn",
          "offUrl": "http://localhost/api/lightOff",

          "statusUrl": "http://localhost/api/lightStatus"
        }   
    ]
}

To get more details about the configuration have a look at the README.

Available characteristics (for the POST body)

Down here are all characteristics listed which can be updated with an request to the homebridge-http-notification-server