Skip to content

Instantly share code, notes, and snippets.

@sweetpi
Last active December 18, 2015 18:45
Show Gist options
  • Save sweetpi/9157350 to your computer and use it in GitHub Desktop.
Save sweetpi/9157350 to your computer and use it in GitHub Desktop.
pimatic: providing devices in a plugin
class MyDevice extends env.devices.Device
actions:
turnOn:
description: "turns the switch on"
turnOff:
description: "turns the switch off"
changeStateTo:
description: "changes the switch to on or off"
params:
state:
type: Boolean
# Returns a promise
turnOn: -> @changeStateTo on
# Retuns a promise
turnOff: -> @changeStateTo off
# Retuns a promise that is fulfilled when done.
changeStateTo: (state) -> #...
inSomeFunctionOfMyPlugin: =>
# Got a new value, so emit it to the framework:
@emit "temperature", 20
class MyDevice extends env.devices.Device
attributes:
temperature:
description: "the messured temperature"
type: Number
unit: '°C'
getTemperature: -> Promise.resolve(30)
class MyDevice extends env.devices.Device
# ####constructor()
# Your constructor function must assign a name and id to the device.
constructor: (@config) ->
@name = @config.name
@id = @config.id
# ...
super()
module.exports = (env) ->
class MyPlugin extends env.plugins.Plugin
init: (app, @framework, config) =>
# ...
deviceConfigDef = require("./device-config-schema")
@framework.deviceManager.registerDeviceClass("MySwitch", {
configDef: deviceConfigDef.MySwitch,
createCallback: (config) => new MySwitch(config)
})
class MySwitch extends env.devices.PowerSwitch
# ...
myPlugin = new MyPlugin
return myPlugin
class MySwitch extends env.devices.PowerSwitch
# ####constructor()
# Your constructor function must assign a name and id to the device.
constructor: (@config) ->
@name = @config.name
@id = @config.id
# ...
class YourTemperatureSensor extends env.devices.TemperatureSensor
temperature: null
constructor: (@config) ->
@name = "some name or from config"
@id = "some-id"
# update the temperature every 5 seconds
setInterval( ( =>
@doYourStuff()
), 5000)
doYourStuff: () ->
# temperature = ...
@temperature = temperature
@emit "temperature", temperature
getTemperature: -> Promise.resolve(@temperature)
class MyCustomDevice extends env.devices.Device
# [...]
getTemplateName: -> "customdevice"
@thost96
Copy link

thost96 commented Dec 18, 2015

There is a typo in the template.coffe. getTemplateName: -> "customdevice"needs to be getTemplate: -> "customdevice".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment