Skip to content

Commit

Permalink
Update domoticz.py
Browse files Browse the repository at this point in the history
  • Loading branch information
DewGew authored Nov 15, 2023
1 parent 3a930f2 commit 3993b9a
Showing 1 changed file with 26 additions and 41 deletions.
67 changes: 26 additions & 41 deletions modules/domoticz.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import re
import modules.config as config

from modules.database import db, User, Settings
from modules.database import User, Settings
from modules.helpers import logger

aogDevs = {}


class AogState:
def __init__(self):
self.id = ''
Expand All @@ -21,11 +22,12 @@ def __init__(self):
self.willReportState = True
self.attributes = {}
self.customData = {}



def AogGetDomain(device):
if device["Type"] not in ['General', 'Lux', 'UV', 'Rain', 'Wind']:
devs = device.get('SwitchType')
if device['Type'] in ['Color Switch', 'Group', 'Scene', 'Temp', 'Thermostat', 'Setpoint','Temp + Humidity', 'Temp + Humidity + Baro']:
if device['Type'] in ['Color Switch', 'Group', 'Scene', 'Temp', 'Thermostat', 'Setpoint', 'Temp + Humidity', 'Temp + Humidity + Baro']:
devs = device["Type"].replace(" ", "")
return devs
if device['Type'] == 'Value' and device['SwitchType'] is None:
Expand All @@ -51,8 +53,8 @@ def getDeviceConfig(descstr):
try:
lines = rawconfig[0].strip().splitlines()
cfgdict = {}
for l in lines:
assign = l.split('=')
for line in lines:
assign = line.split('=')
varname = assign[0].strip().lower()
if varname != "":
if varname in ISLIST:
Expand All @@ -68,7 +70,7 @@ def getDeviceConfig(descstr):
elif varvalue.lower() == "false":
varvalue = False
cfgdict[varname] = varvalue
except:
except Exception:
logger.error('Error parsing device configuration from Domoticz device description:', rawconfig[0])
return None

Expand All @@ -77,7 +79,7 @@ def getDeviceConfig(descstr):


def getAog(device, user_id=None):

dbsettings = Settings.query.get_or_404(1)
domain = AogGetDomain(device)
minThreehold = -20
Expand All @@ -87,8 +89,7 @@ def getAog(device, user_id=None):
domain = domain.replace(" ", "")
domain = domain.replace("/", "")
domain = domain.replace("+", "")



aog = AogState()
aog.id = domain + "_" + device.get('idx')
aog.name = {
Expand All @@ -108,32 +109,15 @@ def getAog(device, user_id=None):
if domain in ['Thermostat', 'Setpoint']:
aog.type = 'action.devices.types.THERMOSTAT'
aog.customData['dzTags'] = False

""" Get additional settings from domoticz description
<voicecontrol>
nicknames = Kitchen Blind One, Left Blind, Blue Blind
room = Kitchen
ack = True
devicetype = oven
minThreehold = -10
maxThreehold = 10
hide = True
camurl = http://livefeed.mycam.com:8080
actual_temp_idx = 345
selector_modes_idx = 346
(supports levelnames: 'off', 'heat', 'cool', 'auto', 'fan-only', 'purifier', 'eco', 'dry')
</voicecontrol>
"""

# Try to get device specific voice control configuration from Domoticz
desc = getDesc(user_id, aog)
if desc is None:
desc = getDeviceConfig(device.get("Description"))
if desc is not None:
logger.info('<voicecontrol> tags found for idx %s in domoticz description.', aog.id)
logger.debug('<voicecontrol> tags found for idx %s in domoticz description.', aog.id)
aog.customData['dzTags'] = True



if desc is not None:
n = desc.get('nicknames', None)
if n is not None:
Expand Down Expand Up @@ -170,15 +154,12 @@ def getAog(device, user_id=None):
hide = desc.get('hide', False)
if hide:
domain = domain +'_Hidden'
# notification = desc.get('notification', False)
# if notification:
# aog.notificationSupportedByAgent = notification


aog.customData['idx'] = device.get('idx')
aog.customData['domain'] = domain
aog.customData['protected'] = device.get('Protected')
aog.notificationSupportedByAgent = (True if domain in ['SmokeDetector', 'Doorbell', 'DoorLock'] else False)

if domain == 'Scene':
aog.type = 'action.devices.types.SCENE'
aog.traits.append('action.devices.traits.Scene')
Expand Down Expand Up @@ -270,7 +251,7 @@ def getAog(device, user_id=None):
}
)
aog.attributes = {'availableToggles': levels}

# Modes trait for selector not working yet -->
if domain == 'Selector_modes':
aog.type = 'action.devices.types.SWITCH'
Expand Down Expand Up @@ -301,7 +282,7 @@ def getAog(device, user_id=None):
)
aog.attributes = {'availableModes': levels}
# <-- Modes trait for selector not working yet

if domain in ['Temp', 'TempHumidity', 'TempHumidityBaro']:
aog.type = 'action.devices.types.SENSOR'
aog.traits.append('action.devices.traits.TemperatureSetting')
Expand All @@ -324,7 +305,7 @@ def getAog(device, user_id=None):
data = getDomoticzState(user_id, aog.customData['selector_modes_idx'])
selectorModes = base64.b64decode(data['LevelNames']).decode('UTF-8').lower().split("|")
aog.attributes['availableThermostatModes'] = selectorModes

if domain == 'MotionSensor':
aog.type = 'action.devices.types.SENSOR'
aog.traits.append('action.devices.traits.OccupancySensing')
Expand All @@ -351,9 +332,10 @@ def getAog(device, user_id=None):
],
'cameraStreamNeedAuthToken': False
}

return aog



# Save device info in json format
def saveJson(user_id, data):

Expand All @@ -366,7 +348,8 @@ def saveJson(user_id, data):
indent=4, ensure_ascii=False)

logger.info('Devices is saved in ' + datafile + ' in ' + config.DEVICES_DIRECTORY + ' folder')



def queryDomoticz(username, url):
user = User.query.filter_by(username=username).first()
domourl = user.domo_url
Expand All @@ -375,11 +358,12 @@ def queryDomoticz(username, url):
try:
r = requests.get(domourl + '/json.htm' + url,
auth=domoCredits, timeout=5.00)
except:
except Exception:
return "{}"

return r.text


def getDomoticzDevices(user_id):

user = User.query.filter_by(username=user_id).first()
Expand All @@ -388,7 +372,7 @@ def getDomoticzDevices(user_id):

try:
r = json.loads(queryDomoticz(user_id, '?type=command&param=getdevices&plan=' + user.roomplan + '&filter=all&used=true'))
except:
except Exception:
logger.error("Error connection to domoticz!")
saveJson(user_id, aogDevs)
return
Expand All @@ -405,6 +389,7 @@ def getDomoticzDevices(user_id):
logger.info('Retreiving devices from domoticz')

saveJson(user_id, aogDevs)


def getDomoticzState(user_id, idx, device='id'):

Expand Down

0 comments on commit 3993b9a

Please sign in to comment.