AquaDMX/aquadmx.py

99 lines
2.7 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
# DMX callback for changes to the universe.
def dmx_callback(data):
light.turn_on(entity_id='light.christian_s_bedroom_left_desk_lamp',
brightness=data[0],
transition=0,
rgb_color=[data[1], data[2], data[3]])
light.turn_on(entity_id='light.christian_s_bedroom_lamp_right',
brightness=data[4],
transition=0,
rgb_color=[data[5], data[6], data[7]])
#
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 getNumChannels(self):
return self.numChannels
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
# 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