homebridge-mvc

MVC Plugin for Homebridge

Usage no npm install needed!

<script type="module">
  import homebridgeMvc from 'https://cdn.skypack.dev/homebridge-mvc';
</script>

README

homebridge-mvc

NPM version

Homebridge-mvc is a Plugin for Homebridge. This Example-plugin is based on MVC (Model-View-Controller) pattern.

Have a look at homebridge-mqtt for a practical implementation.

Installation

If you are new to homebridge, please follow the instructions in homebridge for the homebridge server installation.

Install homebridge-mvc:

sudo npm install -g homebridge-mvc

Configuration

Add the platform in config.json in your home directory inside .homebridge.

{
  "platform": "mvc",
  "name": "mvc"
}

API

  • addAccessory()
  • addService()
  • removeAccessory()
  • removeService()
  • setValue()
  • getAccessories()
  • updateReachability()
  • setAccessoryInformation()
  • get()
  • set()
  • identify()

Howto examples

  1. Define your homebridge platform in index.js:
var platform_name = "myPlatform";

Note: remeber to change config.json for your platform.

  1. Modify the functions for your plugin in model.js

add accessory

accessory = {"name": "flex_lamp", "service_name": "light", "service": "Switch"};
result = addAccessory(accessory);

add service

Note: an accessory with the same name must be added before.

accessory = {"name": "multi_sensor", "service_name": "Humidity", "service": "HumiditySensor"};
result = addService(accessory);

remove accessory

accessory = {"name": "flex_lamp"};
result = removeAccessory(accessory);

remove service

accessory = {"name": "multi_sensor", "service_name": "Humidity"};
result = removeService(accessory);

get accessory/accessories

The purpose of this function is to retrieve accessory definitions.

accessory = {"name": "outdoor_temp"};
result = getAccessories(accessory);
accessory = {"name": "*"};
result = getAccessories(accessory);

set value

accessory = {"name": "flex_lamp", "service_name": "light", "characteristic": "On", "value": true};
result = setValue(accessory);

update reachability


accessory = {"name": "flex_lamp", "reachable": true};
or
accessory = {"name": "flex_lamp", "reachable": false};
result = updateReachability(accessory);

set accessory information

accessory = {"name": "flex_lamp", "manufacturer": "espressif", "model": "esp8266-12", "serialnumber": "4711"};
result = setAccessoryInformation(accessory);

get (from homebridge)

Model.prototype.get = function(name, service_name, characteristic) {...}

set (from homebridge)

Model.prototype.set = function(name, service_name, characteristic, value, callback) {...}

identify (from homebridge)

Model.prototype.identify = function (name, manufacturer, model, serialnumber) {...}

define characterstic

The required characteristics are added with the default properties. If you need to change the default, define the characteristic-name with the properties. e.g.:

accessory = 
  {
    "name": "living_temp",
    "service": "TemperatureSensor",
    "CurrentTemperature": {"minValue": -20, "maxValue": 60, "minStep": 1}
  };
result = addAccessory(accessory);

To add an optional charachteristic define the characteristic-name with "default" or with the properties. e.g.:

accessory = 
  {
    "name": "living_lamp",
    "service": "Lightbulb",
    "Brightness": "default"
  };
result = addAccessory(accessory);
accessory =
  {
    "name": "bathroom_blind",
    "service": "WindowCovering",
    "CurrentPosition": {"minStep": 5},
    "TargetPosition": {"minStep": 5},
    "CurrentHorizontalTiltAngle": {"minValue": 0, "minStep": 5},
    "TargetHorizontalTiltAngle": {"minValue": 0, "minStep": 5}
  };
result = addAccessory(accessory);

HomeKitTypes.js describes all the predifined Services, Characteristcs, format and properties for the value e.g.:

/**
 * Service "Contact Sensor"
 */

Service.ContactSensor = function(displayName, subtype) {
  Service.call(this, displayName, '00000080-0000-1000-8000-0026BB765291', subtype);

  // Required Characteristics
  this.addCharacteristic(Characteristic.ContactSensorState);

  // Optional Characteristics
  this.addOptionalCharacteristic(Characteristic.StatusActive);
  this.addOptionalCharacteristic(Characteristic.StatusFault);
  this.addOptionalCharacteristic(Characteristic.StatusTampered);
  this.addOptionalCharacteristic(Characteristic.StatusLowBattery);
  this.addOptionalCharacteristic(Characteristic.Name);
};

/**
 * Characteristic "Contact Sensor State"
 */

Characteristic.ContactSensorState = function() {
  Characteristic.call(this, 'Contact Sensor State', '0000006A-0000-1000-8000-0026BB765291');
  this.setProps({
    format: Characteristic.Formats.UINT8,
    perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
  });
  this.value = this.getDefaultValue();
};

inherits(Characteristic.ContactSensorState, Characteristic);

Characteristic.ContactSensorState.UUID = '0000006A-0000-1000-8000-0026BB765291';

// The value property of ContactSensorState must be one of the following:
Characteristic.ContactSensorState.CONTACT_DETECTED = 0;
Characteristic.ContactSensorState.CONTACT_NOT_DETECTED = 1;

Derived from this:

service = ContactSensor
characteristic = ContactSensorState
format = UINT8
property = 0 or 1