def execute(self, command, params): """Execute an LockUnlock command.""" domain = self.state.domain state = self.state.state protected = self.state.protected if domain == lockDOMAIN: if params['lock'] == True and state == 'Unlocked': url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=On' elif params['lock'] == False and state == 'Locked': url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=Off' else: raise SmartHomeError(ERR_ALREADY_IN_STATE, 'Unable to execute {} for {}. Already in state '.format(command, self.state.entity_id)) else: if params['lock'] == True and state == 'Unlocked': url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=Off' elif params['lock'] == False and state == 'Locked': url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=On' else: raise SmartHomeError(ERR_ALREADY_IN_STATE, 'Unable to execute {} for {}. Already in state '.format(command, self.state.entity_id)) if protected: url = url + '&passcode=' + configuration['switchProtectionPass'] r = requests.get(url, auth=(configuration['Domoticz']['username'], configuration['Domoticz']['password'])) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError(ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id))
def execute(self, command, params): """Execute a temperature point or mode command.""" # All sent in temperatures are always in Celsius if command == COMMAND_THERMOSTAT_SET_MODE: if self.state.modes_idx is not None: levels = base64.b64decode(self.state.selectorLevelName).decode('UTF-8').split("|") levelName = [x.lower() for x in levels] if params['thermostatMode'] in levelName: level = str(levelName.index(params['thermostatMode']) * 10) url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.modes_idx + '&switchcmd=Set%20Level&level=' + level r = requests.get(url, auth=CREDITS) else: raise SmartHomeError('notSupported', 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id)) if command == COMMAND_THERMOSTAT_TEMPERATURE_SETPOINT: if self.state.modes_idx is not None: levelName = base64.b64decode(self.state.selectorLevelName).decode('UTF-8').split("|") level = self.state.level index = int(level / 10) if levelName[index].lower() == 'off': raise SmartHomeError('inOffMode', 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id)) elif levelName[index].lower() == 'auto': raise SmartHomeError('inAutoMode', 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id)) elif levelName[index].lower() == 'eco': raise SmartHomeError('inEcoMode', 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id)) url = DOMOTICZ_URL + '/json.htm?type=command¶m=setsetpoint&idx=' + self.state.id + '&setpoint=' + str( params['thermostatTemperatureSetpoint']) r = requests.get(url, auth=CREDITS)
def execute(self, command, params): """Execute an ArmDisarm command.""" state = self.state.state seccode = self.state.seccode if params["arm"]: if params["armLevel"] == "Arm Home": if state == "Arm Home": raise SmartHomeError(ERR_ALREADY_IN_STATE, 'Unable to execute {} for {} '.format(command, self.state.entity_id)) else: self.state.state = "Arm Home" url = DOMOTICZ_URL + "/json.htm?type=command¶m=setsecstatus&secstatus=1&seccode=" + seccode if params["armLevel"] == "Arm Away": if state == "Arm Away": raise SmartHomeError(ERR_ALREADY_IN_STATE, 'Unable to execute {} for {} '.format(command, self.state.entity_id)) else: self.state.state = "Arm Away" url = DOMOTICZ_URL + "/json.htm?type=command¶m=setsecstatus&secstatus=2&seccode=" + seccode else: if state == "Normal": raise SmartHomeError(ERR_ALREADY_IN_STATE, 'Unable to execute {} for {} '.format(command, self.state.entity_id)) else: self.state.state = "Normal" url = DOMOTICZ_URL + "/json.htm?type=command¶m=setsecstatus&secstatus=0&seccode=" + seccode r = requests.get(url, auth=CREDITS)
def execute(self, command, params): """Execute an LockUnlock command.""" domain = self.state.domain state = self.state.state protected = self.state.protected if domain == lockDOMAIN: if params['lock'] == True and state == 'Unlocked': url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=On' elif params['lock'] == False and state == 'Locked': url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=Off' else: raise SmartHomeError(ERR_ALREADY_IN_STATE, 'Unable to execute {} for {}. Already in state '.format(command, self.state.entity_id)) else: if params['lock'] == True and state == 'Unlocked': url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=Off' elif params['lock'] == False and state == 'Locked': url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=On' else: raise SmartHomeError(ERR_ALREADY_IN_STATE, 'Unable to execute {} for {}. Already in state '.format(command, self.state.entity_id)) if protected: url = url + '&passcode=' + DOMOTICZ_SWITCH_PROTECTION_PASSWD # print(url) r = requests.get(url, auth=(U_NAME_DOMOTICZ, U_PASSWD_DOMOTICZ)) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError(ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id))
def execute(self, command, params): """Execute an ArmDisarm command.""" state = self.state.state seccode = self.state.seccode if params['arm'] == False: if state == 'Normal': raise SmartHomeError( ERR_ALREADY_IN_STATE, 'Unable to execute {} for {} '.format( command, self.state.entity_id)) else: url = DOMOTICZ_URL + '/json.htm?type=command¶m=setsecstatus&secstatus=0&seccode=' + seccode elif params['arm'] == True: if params['armLevel'] == 'Arm Home': if state == 'Arm Home': raise SmartHomeError( ERR_ALREADY_IN_STATE, 'Unable to execute {} for {} '.format( command, self.state.entity_id)) else: url = DOMOTICZ_URL + '/json.htm?type=command¶m=setsecstatus&secstatus=1&seccode=' + seccode elif params['armLevel'] == 'Arm Away': if state == 'Arm Away': raise SmartHomeError( ERR_ALREADY_IN_STATE, 'Unable to execute {} for {} '.format( command, self.state.entity_id)) else: url = DOMOTICZ_URL + '/json.htm?type=command¶m=setsecstatus&secstatus=2&seccode=' + seccode r = requests.get(url, auth=(U_NAME_DOMOTICZ, U_PASSWD_DOMOTICZ))
def execute(self, command, params): """Execute a OpenClose command.""" features = self.state.attributes protected = self.state.protected state = self.state.state domain = self.state.domain if features & ATTRS_PERCENTAGE: if domain == domains['blindsinv']: url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=Set%20Level&level=' + str( params['openPercent']) else: url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=Set%20Level&level=' + str( 100 - params['openPercent']) else: p = params.get('openPercent', 50) url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=' if domain == domains['blindsinv']: if p == 0 and state in ['Closed', 'Stopped', 'On']: # open url += 'Off' elif p == 100 and state in ['Open', 'Stopped', 'Off']: # close url += 'On' else: raise SmartHomeError( ERR_ALREADY_IN_STATE, 'Unable to execute {} for {}. Already in state '. format(command, self.state.entity_id)) else: if p == 100 and state in ['Closed', 'Stopped', 'On']: # open url += 'Off' elif p == 0 and state in ['Open', 'Stopped', 'Off']: # close url += 'On' else: raise SmartHomeError( ERR_ALREADY_IN_STATE, 'Unable to execute {} for {}. Already in state '. format(command, self.state.entity_id)) if protected: url = url + '&passcode=' + configuration['Domoticz'][ 'switchProtectionPass'] r = requests.get(url, auth=CREDITS) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError( ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'.format( command, self.state.entity_id))
def execute(self, command, params): """Execute a OpenClose command.""" features = self.state.attributes protected = self.state.protected if features & ATTRS_PERCENTAGE: url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=Set%20Level&level=' + str(100-params['openPercent']) else: p = params.get('openPercent', 50) url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=' if p == 100: #open url += 'Off' elif p == 0: #close url += 'On' else: #stop url += 'Stop' if protected: url = url + '&passcode=' + configuration['switchProtectionPass'] r = requests.get(url, auth=(configuration['Domoticz']['username'], configuration['Domoticz']['password'])) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError(ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id))
def can_execute(self, command, params): """Test if command can be executed.""" protected = self.state.protected if protected: raise SmartHomeError('notSupported', 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id)) return command in self.commands
def smarthome_post(self, s): a = s.headers.get('Authorization', None) token = None if a != None: type, tokenH = a.split() if type.lower() == 'bearer': token = Auth['tokens'].get(tokenH, None) if token == None: raise SmartHomeError(ERR_PROTOCOL_ERROR, 'not authorized access!!') return message = json.loads(s.body) print(message) response = self.smarthome_process(message, token) try: if 'errorCode' in response['payload']: print('Error handling message %s: %s' % (message, response['payload'])) except: pass s.send_json(200, json.dumps(response, ensure_ascii=False).encode('utf-8'), True)
def execute(self, command, params): """Execute a OpenClose command.""" protected = self.state.protected p = params.get('openPercent', 50) url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=' if p == 100: #open url += 'Off' elif p == 0: #close url += 'On' else: #stop url += 'Stop' if protected: url = url + '&passcode=' + DOMOTICZ_SWITCH_PROTECTION_PASSWD # print(url) r = requests.get(url, auth=(U_NAME_DOMOTICZ, U_PASSWD_DOMOTICZ)) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError( ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'.format( command, self.state.entity_id))
def execute(self, command, params): """Execute a StartStop command.""" domain = self.state.domain protected = self.state.protected if command == COMMAND_STARTSTOP: if domain == domains['blinds']: if params['start'] is False: url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=Stop' else: url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=' + ( 'On' if params['start'] else 'Off') if protected: url = url + '&passcode=' + configuration['Domoticz'][ 'switchProtectionPass'] r = requests.get(url, auth=CREDITS) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError( ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'. format(command, self.state.entity_id))
def execute(self, command, params): """Execute an SetModes command.""" levelName = base64.b64decode( self.state.selectorLevelName).decode('UTF-8').split("|") protected = self.state.protected for key in params['updateToggleSettings']: if key in levelName: level = str(levelName.index(key) * 10) url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=Set%20Level&level=' + level if protected: url = url + '&passcode=' + configuration['switchProtectionPass'] r = requests.get(url, auth=(configuration['Domoticz']['username'], configuration['Domoticz']['password'])) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError( ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'.format( command, self.state.entity_id))
def execute(self, command, params): """Execute an OnOff command.""" domain = self.state.domain protected = self.state.protected if domain == groupDOMAIN: url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchscene&idx=' + self.state.id + '&switchcmd=' + ( 'On' if params['on'] else 'Off') else: url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=' + ( 'On' if params['on'] else 'Off') if protected: url = url + '&passcode=' + DOMOTICZ_SWITCH_PROTECTION_PASSWD # print(url) r = requests.get(url, auth=(U_NAME_DOMOTICZ, U_PASSWD_DOMOTICZ)) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError( ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'.format( command, self.state.entity_id))
def smarthome_post(self, s): logger.debug(s.headers) a = s.headers.get('Authorization', None) token = None if a is not None: types, tokenH = a.split() if types.lower() == 'bearer': token = Auth['tokens'].get(tokenH, None) if token is None: raise SmartHomeError(ERR_PROTOCOL_ERROR, 'not authorized access!!') message = json.loads(s.body) self._request_id = message.get('requestId') logger.info( "Request " + json.dumps(message, indent=2, sort_keys=False, ensure_ascii=False)) response = self.smarthome_process(message, token) try: if 'errorCode' in response['payload']: logger.info('Error handling message %s: %s' % (message, response['payload'])) except: pass s.send_json(200, json.dumps(response, ensure_ascii=False).encode('utf-8'), True) logger.info("Response " + json.dumps( response, indent=2, sort_keys=False, ensure_ascii=False))
def execute(self, command, params): """Execute an OnOff command.""" domain = self.state.domain protected = self.state.protected if domain != sensorDOMAIN: if domain == groupDOMAIN: url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchscene&idx=' + self.state.id + '&switchcmd=' + ( 'On' if params['on'] else 'Off') else: url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=' + ( 'On' if params['on'] else 'Off') if protected: url = url + '&passcode=' + configuration['switchProtectionPass'] r = requests.get(url, auth=(configuration['Domoticz']['username'], configuration['Domoticz']['password'])) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError( ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'. format(command, self.state.entity_id))
def execute(self, command, params): """Execute a volume command.""" if command == COMMAND_SET_VOLUME: self._execute_set_volume(params) elif command == COMMAND_VOLUME_RELATIVE: self._execute_volume_relative(params) else: raise SmartHomeError(ERR_NOT_SUPPORTED, 'Unable to execute {} for {} '.format(command, self.state.entity_id))
def execute(self, command, params, challenge): """Execute a command. https://developers.google.com/actions/smarthome/create-app#actiondevicesexecute """ executed = False for trt in self.traits(): if trt.can_execute(command, params): acknowledge = self.state.ack # ack is now stored in state pincode = False if configuration['Domoticz']['switchProtectionPass']: protect = self.state.protected else: protect = False if protect or self.state.domain == domains['security']: pincode = configuration['Domoticz']['switchProtectionPass'] if self.state.domain == domains['security']: pincode = self.state.seccode acknowledge = False if challenge is None: raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'pinNeeded', 'Unable to execute {} for {} - challenge needed '.format( command, self.state.entity_id)) elif not challenge.get('pin', False): raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'userCancelled', 'Unable to execute {} for {} - challenge needed '.format( command, self.state.entity_id)) elif True == protect and pincode != challenge.get('pin'): raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'challengeFailedPinNeeded', 'Unable to execute {} for {} - challenge needed '.format( command, self.state.entity_id)) elif self.state.domain == domains['security'] and pincode != hashlib.md5( str.encode(challenge.get('pin'))).hexdigest(): raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'challengeFailedPinNeeded', 'Unable to execute {} for {} - challenge needed '.format( command, self.state.entity_id)) if acknowledge: if challenge is None: raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'ackNeeded', 'Unable to execute {} for {} - challenge needed '.format( command, self.state.entity_id)) elif not challenge.get('ack', False): raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'userCancelled', 'Unable to execute {} for {} - challenge needed '.format( command, self.state.entity_id)) trt.execute(command, params) executed = True break if not executed: raise SmartHomeError(ERR_FUNCTION_NOT_SUPPORTED, 'Unable to execute {} for {}'.format(command, self.state.entity_id))
def execute(self, command, params, challenge): """Execute a command. https://developers.google.com/actions/smarthome/create-app#actiondevicesexecute """ executed = False for trt in self.traits(): if trt.can_execute(command, params): ack = False pin = False desc = getDesc(self.state) if desc != None: ack = desc.get('ack', False) if DOMOTICZ_SWITCH_PROTECTION_PASSWD != False: protect = self.state.protected else: protect = False if protect or self.state.domain == securityDOMAIN: pin = DOMOTICZ_SWITCH_PROTECTION_PASSWD if self.state.domain == securityDOMAIN: pin = self.state.seccode ack = False if challenge == None: raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'pinNeeded', 'Unable to execute {} for {} - challenge needed '.format(command, self.state.entity_id)) elif False == challenge.get('pin', False): raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'userCancelled', 'Unable to execute {} for {} - challenge needed '.format(command, self.state.entity_id)) elif True == protect and pin != challenge.get('pin'): raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'challengeFailedPinNeeded', 'Unable to execute {} for {} - challenge needed '.format(command, self.state.entity_id)) elif self.state.domain == securityDOMAIN and pin != hashlib.md5(str.encode(challenge.get('pin'))).hexdigest(): raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'challengeFailedPinNeeded', 'Unable to execute {} for {} - challenge needed '.format(command, self.state.entity_id)) if ack: print(challenge) if challenge == None: raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'ackNeeded', 'Unable to execute {} for {} - challenge needed '.format(command, self.state.entity_id)) elif False == challenge.get('ack', False): raise SmartHomeErrorNoChallenge(ERR_CHALLENGE_NEEDED, 'userCancelled', 'Unable to execute {} for {} - challenge needed '.format(command, self.state.entity_id)) trt.execute(command, params) executed = True break if not executed: raise SmartHomeError(ERR_FUNCTION_NOT_SUPPORTED, 'Unable to execute {} for {}'.format(command, self.state.entity_id))
def execute(self, command, params): """Execute a brightness command.""" protected = self.state.protected url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=Set%20Level&level=' + str(int(params['brightness'] * self.state.maxdimlevel / 100)) if protected: url = url + '&passcode=' + configuration['switchProtectionPass'] r = requests.get(url, auth=(configuration['Domoticz']['username'], configuration['Domoticz']['password'])) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError(ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id))
def execute(self, command, params): """Execute a scene command.""" protected = self.state.protected url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchscene&idx=' + self.state.id + '&switchcmd=On' if protected: url = url + '&passcode=' + configuration['Domoticz']['switchProtectionPass'] r = requests.get(url, auth=CREDITS) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError(ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id))
def execute(self, command, params): """Execute a brightness command.""" #domain = self.state.domain protected = self.state.protected url = DOMOTICZ_URL + '/json.htm?type=command¶m=switchlight&idx=' + self.state.id + '&switchcmd=Set%20Level&level=' + str(int(params['brightness'] * self.state.maxdimlevel / 100)) if protected: url = url + '&passcode=' + DOMOTICZ_SWITCH_PROTECTION_PASSWD # print(url) r = requests.get(url, auth=(U_NAME_DOMOTICZ, U_PASSWD_DOMOTICZ)) if protected: status = r.json() err = status.get('status') if err == 'ERROR': raise SmartHomeError(ERR_WRONG_PIN, 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id))
def execute(self, command, params): """Execute a Timer command.""" version = self.state.dzvents # date_str = datetime.strptime(self.state.lastupdate, "%Y-%m-%d %H:%M:%S") # timestamp = datetime.timestamp(date_str) # print("date =", date_str) # print("timestamp =", timestamp) if version is not None and version >= '3.0.0': if command == COMMAND_TIMER_START: url = DOMOTICZ_URL + '/json.htm?type=command¶m=customevent&event=TIMER&data={"idx":' + self.state.id + ',"time":' + str(params['timerTimeSec']) + ',"on":true}' r = requests.get(url, auth=CREDITS) if command == COMMAND_TIMER_CANCEL: url = DOMOTICZ_URL + '/json.htm?type=command¶m=customevent&event=TIMER&data={"idx":' + self.state.id + ',"cancel":true}' r = requests.get(url, auth=CREDITS) else: logger.error('To use Timer function you need to run Domoticz version above 2020.1.11804') logger.error('and have dzVents Dzga_Timer script installed and active') raise SmartHomeError('functionNotSupported', 'Unable to execute {} for {} check your settings'.format(command, self.state.entity_id))