Skip to content

Instantly share code, notes, and snippets.

@CliffS
Created March 12, 2018 11:18
Show Gist options
  • Save CliffS/ce8ded8b447a04be8dd6f139bf7224b8 to your computer and use it in GitHub Desktop.
Save CliffS/ce8ded8b447a04be8dd6f139bf7224b8 to your computer and use it in GitHub Desktop.
milight module
Constants = require '../local/Constants'
Controller = require('node-milight-promise').MilightController
commandsV6 = require('node-milight-promise').commandsV6
milight = commandsV6.fullColor
discoverBridges = require('node-milight-promise').discoverBridges
log = Constants.log 'milight'
class Milight
constructor: (@zone = 1) ->
connect: ->
bridge = await discoverBridges type: 'v6'
@light = new Controller
ip: bridge[0].ip
type: bridge[0].type
@light.ready()
.then =>
log.debug 'Milight is ready'
on: ->
log.debug "zone #{@zone}: switched on"
@light.sendCommands milight.on(@zone), milight.brightness 100
off: ->
log.debug "zone #{@zone}: switched off"
@light.sendCommands milight.off @zone
white: ->
log.debug "zone #{@zone}: set to bright white"
@light.sendCommands milight.on(@zone), milight.whiteMode @zone
temperature: (level) -> # 2700K ... 6500K
level = parseInt level
if 2700 <= level <= 6500
temp = Math.floor((level - 2700) * 0x64 / 3800)
else if 0x00 <= level <= 0x64
temp = level
else
throw new RangeError "Colour temperature between 2700K and 6500K"
log.debug "zone #{@zone}: temperature set to #{temp}"
@light.sendCommands milight.whiteTemperature @zone, temp
brightness: (percent) ->
percent = parseInt percent
unless 0 <= percent <= 100
throw new RangeError "Brightness must be between 0 and 100"
log.debug "zone #{@zone}: brightness set to #{percent}"
@light.sendCommands milight.on(@zone), milight.brightness @zone, percent
colour: (rgb) ->
rgb = parseInt rgb, 16 if typeof rgb is 'string'
throw new RangeError "Wrong RGB" unless 0x0000 <= rgb <= 0xffff
red = rgb >>> 16
green = (rgb >>> 8) & 0xff
blue = rgb & 0xff
@light.sendCommands milight.on(@zone), milight.rgb @zone, red, green, blue
close: ->
@light.close()
module.exports = Milight
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment