AquaDMX/aquadmx.py

126 lines
3.5 KiB
Python

import configparser
from enum import Enum
import time
import os
from stupidArtnet import StupidArtnetServer
from homeassistant_api import Processing, Client
from homeassistant_api.processing import process_json
# Globals
dataBuffer = []
def setLight(device, data):
start = device.getStartChannel()
print('{}'.format(device.getDeviceID()))
print(data[start], data[start+1], data[start+2], data[start+3])
if device.getLightType() == LightType.DIMMER:
light.turn_on(entity_id=device.getDeviceID(),
brightness=data[start],
transition=0)
elif device.getLightType() == LightType.RGB:
light.turn_on(entity_id=device.getDeviceID(),
brightness=data[start],
transition=0,
rgb_color=[data[start+1], data[start+2], data[start+3]])
# DMX callback for changes to the universe.
def dmx_callback(data):
global dataBuffer
if dataBuffer:
diffs = [i[0] for i in enumerate(data) if data[i[0]] != dataBuffer[i[0]]]
for d in u1.getDevices():
setLight(d, data)
else:
# Set all lights on first
for d in u1.getDevices():
setLight(d, data)
dataBuffer = data
#
def configList(config, key):
return [i.strip(' ') for i in config[key].split('\n')]
# Returns a list of config items.
def configOptionalList(config, key):
if key in config:
return configList(config, key)
else:
return []
class LightType(Enum):
DIMMER = 1
RGB = 2
class DMXDevice(object):
startChannel = 0
def __init__(self, deviceID, lightType):
self.deviceID = deviceID
self.lightType = lightType
if lightType is LightType.DIMMER:
self.numChannels = 1
elif lightType is LightType.RGB:
self.numChannels = 4
def getNumChannels(self):
return self.numChannels
def setStartChannel(self, start):
self.startChannel = start
def getStartChannel(self):
return self.startChannel
def getNumChannels(self):
return self.numChannels
def getLightType(self):
return self.lightType
def getDeviceID(self):
return self.deviceID
class DMXUniverse(object):
maxChannels = 512
def __init__(self):
self.channels = 1
self.devices = []
def addDevice(self, device):
device.setStartChannel(self.channels)
self.devices.append(device)
self.channels += device.getNumChannels()
def getChannels(self):
return self.channels
def getDevices(self):
return self.devices
# Configs
config = configparser.ConfigParser()
config.sections()
programPath = os.path.dirname(os.path.realpath(__file__))
config.read('{}/settings.ini'.format(programPath))
# Home Assistant
URL = '{}/api'.format(config['HomeAssistant']['url'])
TOKEN = config['HomeAssistant']['token']
client = Client(URL, TOKEN)
light = client.get_domain('light')
# Lights
u1 = DMXUniverse()
dimmers = configOptionalList(config['Lights'], 'dimmer')
for l in dimmers:
u1.addDevice(DMXDevice(l, LightType.DIMMER))
rgbs = configOptionalList(config['Lights'], 'rgb')
for l in rgbs:
u1.addDevice(DMXDevice(l, LightType.RGB))
print(u1.getChannels())
# DMX
universe = 1
server = StupidArtnetServer()
u1_listener = server.register_listener(universe,
callback_function=dmx_callback)
# Start server
print('AquaDMX is listening')
while True:
time.sleep(1)
pass