npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

homebridge-http-securitysystem

v1.0.8

Published

HTTP based SecuritySystem plugin for homebridge: https://github.com/nfarina/homebridge, with readable state

Downloads

16

Readme

homebridge-http-securitysystem

Homebridge plugin that creates a SecuritySystem device which uses configurable HTTP calls to set and get its state.

It's main purpose is to connect security solutions that are not HomeKit compatible to HomeKit.

This plugin can be used as a security system in HomeKit/Homebridge. It creates a Homebridge accessory which uses HTTP calls to arm, disarm and check the status of security systems and provides the Service.SecuritySystem service to HomeKit with both the SecuritySystemCurrentState and the SecuritySystemTargetState characteristics implemented.

Installation

  1. Install homebridge using: npm install -g homebridge
  2. Install homebridge-http-securitysystem using: npm install -g homebridge-http-securitysystem
  3. Update your configuration file. See sample-config.json in this repository for a sample.

Features

The main function of the module is to proxy HomeKit queries to an arbitrary web API to retrieve and set the status of the security system. Main features include:

  • Configurable HTTP endpoints to use for getting/setting the state, including passing parameters in for of GET or in POST body
  • Support for basic HTTP authentication
  • Configurable mapping of API response data to HomeKit SecurityDevice status to allow custom responses
  • Interval polling of the current state to enable real-time notifications even if the security system has been enabled without the use of HomeKit

Configuration

This module requires that the URLs for getting and setting the security system's state are configured correctly. This has to be done in Homebridge's config.json. You can find a sample configuration file in this repository.

The configuration options are the following:

Configuration example with explanation

    "accessories": [
        {
            "accessory": "Http-SecuritySystem",
            "name": "Home security",
            "debug": false,
            "username": "",
            "password": "",
            "immediately": false,
            "polling": true,
            "pollInterval": 30000,
            "http_method": "POST",
            "urls": {
                "stay": { "url": "http://localhost:1880/alarm/arm", "body": "stay" },
                "away": { "url": "http://localhost:1880/alarm/arm", "body": "away" },
                "night": { "url": "http://localhost:1880/alarm/arm", "body": "night" },
                "disarm": { "url": "http://localhost:1880/alarm/disarm", "body": "" },
                "readCurrentState": { "url": "http://localhost:1880/alarm/check", "body": "" },
                "readTargetState": { "url": "http://localhost:1880/alarm/check", "body": "", "headers": { "Content-Type", "application/json" } }
            },
            "mappers": [
                {
                    "type": "xpath",
                    "parameters": {
                        "xpath": "//partition[3]/text()"
                    }
                },
                {
                    "type": "regex",
                    "parameters": {
                        "regexp": "^The system is currently (ARMED|DISARMED), yo!$",
                        "capture": "1"
                    }
                },
                {
                    "type": "static",
                    "parameters": {
                        "mapping": {
                            "ARMED": "0",
                            "DISARMED": "3"
                        }
                    }
                }
            ]
        }
    ]
  • The name parameter determines the name of the security system you will see in HomeKit.
  • The username/password configuration can be used to specify the username and password if the remote webserver requires HTTP authentication.
  • debug turns on debug messages. The important bit is that it reports the mapping process so that it's easier to debug
  • The http_method can be either "GET" or "POST". The HTTP requests going to the target webserver will be using this method.
  • The urls section configures the URLs that are to be called on certain events.
    • The stay, away and night URLs are called when HomeKit is instructed to arm the alarm (it has 3 different alarm on states)
    • The disarm URL is used when HomeKit is instructed to disarm the alarm
    • The readCurrentState and readTargetState are used by HomeKit for querying the current state of the alarm device. It should return the following values in the body of the HTTP response:
      • "0": stay armed
      • "1": away armed
      • "2": night armed
      • "3": disarmed
      • "4": alarm has been triggered
    • if you need to call multiple URLs for changing the state, you can pass in an array of URL definitions instead of a single object
  • The polling is a boolean that specifies if the current state should be pulled on regular intervals or not. Defaults to false.
  • pollInterval is a number which defines the poll interval in milliseconds. Defaults to 30000.
  • The mappings optional parameter allows the definition of several response mappers. This can be used to translate the response received by readCurrentState and readTargetState to the expect 0...4 range expected by homekit

URL configuration

When defining URLs in the config, you have to the URL to hit and the body to post to that URL (which can be empty of course). An URL definition can be:

  • either a single configuration object
  • or an array of configuration objects

Each configuration object has the following properties:

  • url: the URL to hit
  • body: the request body to send to the URL (optional)
  • headers: an object, of which the property-value pairs will be sent as HTTP headers.

Multiple endpoints are only supported for the setTargetState endpoint. The other calls always go to a single URL only.

Response mapping

The mappings block of the configuration may contain any number of mapper definitions. The mappers are chained after each other, the result of a mapper is fed into the input of the next mapper. The purpose of this whole chain is to somehow boil down the response received from the API to a single number which is expected by Homekit.

Each mapper has the following JSON format:

{
    "type": "<type of the mapper>",
    "parameters": { <parameters to be passed to the mapper> }
}

There are 3 kinds of mappers implemented at the moment.

Static mapper

The static mapper can be used to define a key => value dictionary. It will simply look up the input in the dictionary and if it is found, it returns the corresponding value. It's great for mapping string responses like "ARMED" to their actual number.

Configuration is as follows:

{
    "type": "static",
    "parameters": { 
        "mapping": {
            "STAY": "0",
            "AWAY": "1",
            "whataever you don't like": "whatever you like more"
        }
    }
}

This configuration would map STAY to 0, AWAY to 1 and "whatever you don't like" to "whatever you like more". If the mapping does not have an entry which corresponds to input, it returns the full input.

Regexp mapper

The regexp mapper can be used to define a regular expression to run on the input, capture some substring of it and return it. It's great for mapping string responses which may change around but have a certain part that's always there and which is the part you are interested in.

Configuration is as follows:

{
    "type": "regex",
    "parameters": {
        "regexp": "^The system is currently (ARMED|DISARMED), yo!$",
        "capture": "1"
    }
}

This configuration will run the regular expression defined by the regexp parameter against the input and return the first capture group (as defined by capture). So, in this case, if the input is "The system is currenty ARMED, yo!", the mapper will map this to "ARMED".

If the regexp does not match the input, the mapper returns the full input.

XPath mapper

The XPath mapper can be used to extract data from an XML document. It allows the definition of an XPath which will then be applied to the input and returns whatever the query selects.

When using this mapper, make sure that you select text elements and not entire nodes or node lists, otherwise it will fail horribly.

Configuration is as follows:

{
    "type": "xpath",
    "parameters": {
        "xpath": "//partition[3]/text()",
        "index": 0
    }
}

Let's assume this mapper gets the following input:

<?xml version="1.0" encoding="ISO-8859-1"?>
<partitionsStatus>
    <partition>ARMED</partition>
    <partition>ARMED</partition>
    <partition>ARMED_IMMEDIATE</partition>
</partitionsStatus>

In this case this mapper will return "ARMED_IMMEDIATE". The index parameter can be used to specify which element to return if the xpath selects multiple elements. In the example above it is completely redundant as partition[3] already makes sure that a single partition is selected.