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.
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
Add the platform in config.json in your home directory inside .homebridge
.
{
"platform": "mvc",
"name": "mvc"
}
- addAccessory()
- addService()
- removeAccessory()
- removeService()
- setValue()
- getAccessories()
- updateReachability()
- setAccessoryInformation()
- get()
- set()
- identify()
- Define your homebridge platform in index.js:
var platform_name = "myPlatform";
Note: remeber to change config.json
for your platform.
- Modify the functions for your plugin in model.js
accessory = {"name": "flex_lamp", "service_name": "light", "service": "Switch"};
result = addAccessory(accessory);
Note: an accessory with the same name must be added before.
accessory = {"name": "multi_sensor", "service_name": "Humidity", "service": "HumiditySensor"};
result = addService(accessory);
accessory = {"name": "flex_lamp"};
result = removeAccessory(accessory);
accessory = {"name": "multi_sensor", "service_name": "Humidity"};
result = removeService(accessory);
The purpose of this function is to retrieve accessory definitions.
accessory = {"name": "outdoor_temp"};
result = getAccessories(accessory);
accessory = {"name": "*"};
result = getAccessories(accessory);
accessory = {"name": "flex_lamp", "service_name": "light", "characteristic": "On", "value": true};
result = setValue(accessory);
accessory = {"name": "flex_lamp", "reachable": true};
or
accessory = {"name": "flex_lamp", "reachable": false};
result = updateReachability(accessory);
accessory = {"name": "flex_lamp", "manufacturer": "espressif", "model": "esp8266-12", "serialnumber": "4711"};
result = setAccessoryInformation(accessory);
Model.prototype.get = function(name, service_name, characteristic) {...}
Model.prototype.set = function(name, service_name, characteristic, value, callback) {...}
Model.prototype.identify = function (name, manufacturer, model, serialnumber) {...}
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