Update only light changes
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
			
		||||
# AquaDMX
 | 
			
		||||
 | 
			
		||||
A custom DMX server that integrates with [Home Assistant](home-assistant.io).
 | 
			
		||||
A custom DMX server that integrates with [Home Assistant](https://home-assistant.io).
 | 
			
		||||
 | 
			
		||||
## Setup
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										54
									
								
								aquadmx.py
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								aquadmx.py
									
									
									
									
									
								
							@@ -11,10 +11,9 @@ from homeassistant_api.processing import process_json
 | 
			
		||||
# Globals
 | 
			
		||||
dataBuffer = []
 | 
			
		||||
 | 
			
		||||
# Set Home Assistant Light based on DMX device and data.
 | 
			
		||||
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],
 | 
			
		||||
@@ -29,8 +28,8 @@ def setLight(device, data):
 | 
			
		||||
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():
 | 
			
		||||
        diffs = [i[0]+1 for i in enumerate(data) if data[i[0]] != dataBuffer[i[0]]]
 | 
			
		||||
        for d in u1.getDevicesByID(diffs):
 | 
			
		||||
            setLight(d, data)
 | 
			
		||||
    else:
 | 
			
		||||
        # Set all lights on first
 | 
			
		||||
@@ -38,7 +37,7 @@ def dmx_callback(data):
 | 
			
		||||
            setLight(d, data)
 | 
			
		||||
    dataBuffer = data
 | 
			
		||||
 | 
			
		||||
#
 | 
			
		||||
# Get a list of items in a config key
 | 
			
		||||
def configList(config, key):
 | 
			
		||||
    return [i.strip(' ') for i in config[key].split('\n')]
 | 
			
		||||
 | 
			
		||||
@@ -49,12 +48,17 @@ def configOptionalList(config, key):
 | 
			
		||||
    else:
 | 
			
		||||
        return []
 | 
			
		||||
 | 
			
		||||
# Types of Lights Supported.
 | 
			
		||||
class LightType(Enum):
 | 
			
		||||
    DIMMER = 1
 | 
			
		||||
    RGB = 2
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
# Data structure for a DMX Device.
 | 
			
		||||
class DMXDevice(object):
 | 
			
		||||
 | 
			
		||||
    startChannel = 0
 | 
			
		||||
 | 
			
		||||
    # Initialize DMX Device.
 | 
			
		||||
    def __init__(self, deviceID, lightType):
 | 
			
		||||
        self.deviceID = deviceID
 | 
			
		||||
        self.lightType = lightType
 | 
			
		||||
@@ -62,34 +66,65 @@ class DMXDevice(object):
 | 
			
		||||
            self.numChannels = 1
 | 
			
		||||
        elif lightType is LightType.RGB:
 | 
			
		||||
            self.numChannels = 4
 | 
			
		||||
 | 
			
		||||
    # Returns number of channels the device uses.
 | 
			
		||||
    def getNumChannels(self):
 | 
			
		||||
        return self.numChannels
 | 
			
		||||
 | 
			
		||||
    # Set the start channel for the device.
 | 
			
		||||
    def setStartChannel(self, start):
 | 
			
		||||
        self.startChannel = start
 | 
			
		||||
 | 
			
		||||
    # Get the start channel for the device.
 | 
			
		||||
    def getStartChannel(self):
 | 
			
		||||
        return self.startChannel
 | 
			
		||||
    def getNumChannels(self):
 | 
			
		||||
        return self.numChannels
 | 
			
		||||
 | 
			
		||||
    # Get the end channel for the device.
 | 
			
		||||
    def getEndChannel(self):
 | 
			
		||||
        return self.startChannel+self.numChannels-1
 | 
			
		||||
 | 
			
		||||
    # Get the type of light.
 | 
			
		||||
    def getLightType(self):
 | 
			
		||||
        return self.lightType
 | 
			
		||||
 | 
			
		||||
    # Get the device ID for Home Assistant.
 | 
			
		||||
    def getDeviceID(self):
 | 
			
		||||
        return self.deviceID
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
# Data structure for a DMX Universe.
 | 
			
		||||
class DMXUniverse(object):
 | 
			
		||||
 | 
			
		||||
    maxChannels = 512
 | 
			
		||||
 | 
			
		||||
    # initialize DMX Universe.
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        self.channels = 1
 | 
			
		||||
        self.devices = []
 | 
			
		||||
 | 
			
		||||
    # Add a device to the DMX Universe.
 | 
			
		||||
    def addDevice(self, device):
 | 
			
		||||
        device.setStartChannel(self.channels)
 | 
			
		||||
        self.devices.append(device)
 | 
			
		||||
        self.channels += device.getNumChannels()
 | 
			
		||||
 | 
			
		||||
    # Get channels in use in the DMX Universe.
 | 
			
		||||
    def getChannels(self):
 | 
			
		||||
        return self.channels
 | 
			
		||||
 | 
			
		||||
    # Get a list of devices in the DMX Universe.
 | 
			
		||||
    def getDevices(self):
 | 
			
		||||
        return self.devices
 | 
			
		||||
 | 
			
		||||
    # Get a list of device in the DMX Universe that match the given
 | 
			
		||||
    # list of channels.
 | 
			
		||||
    def getDevicesByID(self, channels):
 | 
			
		||||
        l = []
 | 
			
		||||
        for d in self.devices:
 | 
			
		||||
            for c in channels:
 | 
			
		||||
                if d.getStartChannel() <= c and c <= d.getEndChannel():
 | 
			
		||||
                    l.append(d)
 | 
			
		||||
        return set(l)
 | 
			
		||||
 | 
			
		||||
# Configs
 | 
			
		||||
config = configparser.ConfigParser()
 | 
			
		||||
config.sections()
 | 
			
		||||
@@ -110,7 +145,6 @@ for l in dimmers:
 | 
			
		||||
rgbs = configOptionalList(config['Lights'], 'rgb')
 | 
			
		||||
for l in rgbs:
 | 
			
		||||
    u1.addDevice(DMXDevice(l, LightType.RGB))
 | 
			
		||||
print(u1.getChannels())
 | 
			
		||||
 | 
			
		||||
# DMX
 | 
			
		||||
universe = 1
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user