def _is_parameters_ok(self): """ Check if received links are ok to perform operations :return: true if the neuron is well configured, raise an exception otherwise .. raises:: MissingParameterException, InvalidParameterException """ # with the neuron the user has the choice of a direct link that call another synapse, # or a link with an answer caught from the STT engine # we cannot use at the same time a direct redirection and a link with question if self.direct_link is not None and self.from_answer_link is not None: raise InvalidParameterException( "neurotransmitter cannot be used with both direct_link and from_answer_link" ) if self.direct_link is None and self.from_answer_link is None: raise MissingParameterException( "neurotransmitter must be used with direct_link or from_answer_link" ) if self.from_answer_link is not None: if self.default is None: raise InvalidParameterException( "default parameter is required and must contain a valid synapse name" ) for el in self.from_answer_link: if "synapse" not in el: raise MissingParameterException( "Links must contain a synapse name: %s" % el) if "answers" not in el: raise MissingParameterException( "Links must contain answers: %s" % el) return True
def _is_parameters_ok(self): """ Check that all provided parameters in the neurons are valid :return: True if all check passed """ # URL is mandatory if self.url is None: raise InvalidParameterException("Uri needs an url") # headers can be null, but if provided it must be a list if self.headers is not None: if not isinstance(self.headers, dict): raise InvalidParameterException("headers must be a list of key: value") # timeout in second must be an integer if self.timeout is not None: if not isinstance(self.timeout, int): raise InvalidParameterException("timeout must be an integer") # data must be loadable with json if self.data is not None: try: json.loads(self.data) except ValueError, e: raise InvalidParameterException("error in \"data\" parameter: %s" % e)
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.query is None or self.query == "": raise InvalidParameterException("Wikipedia needs a query") if self.language is None: raise InvalidParameterException("Wikipedia needs a language") valid_language = wikipedia.languages().keys() if self.language not in valid_language: raise InvalidParameterException( "Wikipedia needs a valid language: %s" % valid_language) if self.sentences is not None: try: self.sentences = int(self.sentences) except ValueError: # Handle the exception raise InvalidParameterException( "Number of sentences must be an integer") return True
def _is_parameters_ok(self): """ Check that all given parameter are valid :return: True if all given parameter are ok """ if self.state not in ["on", "off"]: raise InvalidParameterException("[Ambient_sounds] State must be 'on' or 'off'") # check that the given sound name exist if self.sound_name is not None: self.target_ambient_sound = self.sdb.get_sound_by_name(self.sound_name) if self.target_ambient_sound is None: raise InvalidParameterException("[Ambient_sounds] Sound name %s does not exist" % self.sound_name) # if wait auto_stop_minutes is set, mut be an integer or string convertible to integer if self.auto_stop_minutes is not None: if not isinstance(self.auto_stop_minutes, int): try: self.auto_stop_minutes = int(self.auto_stop_minutes) except ValueError: raise InvalidParameterException("[Ambient_sounds] auto_stop_minutes must be an integer") # check auto_stop_minutes is positive if self.auto_stop_minutes < 1: raise InvalidParameterException("[Ambient_sounds] auto_stop_minutes must be set at least to 1 minute") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: MissingParameterException """ if self.sensor_type is None: raise MissingParameterException( "You must specify a sensor_type. Can be 'DHT11', 'DHT22' or 'AM2302'" ) if self.sensor_type not in supported_sensor: raise InvalidParameterException( "sensor_type must be 'DHT11', 'DHT22' or 'AM2302'") if self.pin is None: raise MissingParameterException( "You must specify a pin number (BCM)") # check that is an integer try: self.pin = int(self.pin) except ValueError: raise InvalidParameterException("pin must be a valid integer") return True
def _is_parameters_ok(self): if self.mm_url is None: raise InvalidParameterException("Missing mm_url parameter") if self.notification is None: raise InvalidParameterException("Missing notification parameter") if self.payload is None: raise InvalidParameterException("Missing payload parameter") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.configuration['url'] is None: raise InvalidParameterException("CalDav requires an URL.") if self.configuration['action'] is None: raise InvalidParameterException("CalDav requires an action.") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.configuration['client_id'] is None: raise InvalidParameterException("Twitch tv requires a client ID") if self.configuration['user'] is None: raise InvalidParameterException("User required for Twitch TV") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron. :return: True if parameters are ok, raise an exception otherwise. .. raises:: MissingParameterException, InvalidParameterException """ if self.url is None: raise MissingParameterException("Webbrowser needs an url") if self.url_check(self.url) is False: raise InvalidParameterException("Webbrowser: invalid url") if self.option not in OPTIONS: raise InvalidParameterException("Webbrowser: invalid option") return True
def _is_volume_parameters_ok(self): if self.volume is None: raise MissingParameterException(self.action + " action needs a volume parameter") try: value = int(self.volume) except ValueError: raise InvalidParameterException( "volume parameter must be integer 0..100") if value < 0 or value > 100: raise InvalidParameterException( "volume parameter must be integer 0..100") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: MissingParameterException, InvalidParameterException """ if self.path is None: raise MissingParameterException("You must provide a script path.") if not os.path.isfile(self.path): raise InvalidParameterException("Script not found or is not a file.") if not os.access(self.path, os.X_OK): raise InvalidParameterException("Script not Executable.") return True
def check_for_integer(parameter): if isinstance(parameter, list): for item in parameter: if isinstance(item, (int)): continue else: raise InvalidParameterException( "[Gpio] %s List contains not valid integers" % parameter) else: try: parameter = int(parameter) except ValueError: raise InvalidParameterException( "[Gpio] %s is not a valid integer" % parameter)
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.configuration['gmaps_api_key'] is None: raise InvalidParameterException("Google Maps require an API key") if self.configuration['destination'] is None and self.configuration[ 'search'] is None: raise InvalidParameterException( "Google Maps needs a destination or place name") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.configuration['uber_api_key'] is None: raise InvalidParameterException( "Uber neuronrequire an Uber API key") if self.configuration['start_address'] is None \ and (self.configuration['start_longitude'] is None or self.configuration['start_latitude'] is None): raise InvalidParameterException( "Missing start_address or start longitude and latitude") return True
def _is_parameters_ok(self): if self.level is None: raise InvalidParameterException("[Volume] level need to be set") try: self.level = int(self.level) except ValueError: raise InvalidParameterException( "[Volume] level '{}' is not a valid integer".format( self.level)) if self.level < 0 or self.level > 100: raise InvalidParameterException( "[Volume] level need to be placed between 0 and 100") if self.action not in ["set", "raise", "lower"]: raise InvalidParameterException( "[Volume] action can be 'set', 'raise' or 'lower'") return True
def _is_parameters_ok(self): """ Check that all provided parameters in the neurons are valid :return: True if all check passed """ # URL is mandatory if self.url is None: raise InvalidParameterException("Uri needs an url") # headers can be null, but if provided it must be a list if self.headers is not None: if not isinstance(self.headers, dict): raise InvalidParameterException( "headers must be a list of key: value") # timeout in second must be an integer if self.timeout is not None: if not isinstance(self.timeout, int): raise InvalidParameterException("timeout must be an integer") # data must be loadable with json if self.data is not None: try: json.loads(self.data) except ValueError as e: raise InvalidParameterException( "error in \"data\" parameter: %s" % e) # data_from_file path must exist and data inside must be loadable by json if self.data_from_file is not None: # check that the file exist if not os.path.exists(self.data_from_file): raise InvalidParameterException( "error in \"data_file\". File does not exist: %s" % self.data_from_file) # then try to load the json from the file try: self.data_from_file = self.readfile(self.data_from_file) except ValueError as e: raise InvalidParameterException( "error in \"data\" parameter: %s" % e) # we cannot provide both data and data from file if self.data is not None and self.data_from_file is not None: raise InvalidParameterException( "URI can be used with data or data_from_file, not both in same time" ) # the provided method must exist allowed_method = [ "GET", "POST", "DELETE", "PUT", "HEAD", "PATCH", "OPTIONS" ] if self.method not in allowed_method: raise InvalidParameterException("method %s not in: %s" % (self.method, allowed_method)) return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.configuration['gmaps_api_key'] is None: raise InvalidParameterException("Google Maps require an API key") if self.configuration['local'] is None: raise InvalidParameterException("WW Time needs a local") if self.configuration['city'] is None: raise InvalidParameterException("WW Time needs a city") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.lang_out is None: raise InvalidParameterException("Needs Lang out") if self.lang_in is None: raise InvalidParameterException("Needs Lang in") if self.sentence is None: raise InvalidParameterException("Needs sentence") return True
def _is_search_parameters_ok(self): if self.query is None: raise MissingParameterException(self.action + " action needs a query parameter") if self.search_type not in Search_Types: raise InvalidParameterException("Invalid search_type parameter") return True
def _is_parameters_ok(self): if self.action is None: raise MissingParameterException( "Spotify needs an action parameter") if self.action not in Spotify_Actions: raise InvalidParameterException("Invalid action parameter") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.configuration['url'] is None: raise InvalidParameterException("Web scraper needs a url") if self.configuration['main_selector']['tag'] is None: raise InvalidParameterException( "Web scraper needs a main_selector tag") if self.configuration['main_selector']['class'] is None: raise InvalidParameterException( "Web scraper needs a main_selector class") if self.configuration['title_selector']['tag'] is None: raise InvalidParameterException( "Web scraper needs a title_selector tag") if self.configuration['title_selector']['class'] is None: raise InvalidParameterException( "Web scraper needs a title_selector class") if self.configuration['description_selector']['tag'] is None: raise InvalidParameterException( "Web scraper needs a description_selector tag") if self.configuration['description_selector']['class'] is None: raise InvalidParameterException( "Web scraper needs a description_selector class") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.configuration['mpd_url'] is None: raise InvalidParameterException("MPD needs a url") if self.configuration['mpd_action'] is None: raise InvalidParameterException("MPD needs an action") elif self.configuration['mpd_action'] in ['playlist', 'playlist_spotify', 'search', 'file'] \ and self.configuration['query'] is None: raise InvalidParameterException( "MPD requires a query for this action") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.query is None: raise InvalidParameterException("Wikipedia needs a query") if self.language is None: raise InvalidParameterException("Wikipedia needs a language") valid_language = wikipedia.languages().keys() if self.language not in valid_language: raise InvalidParameterException( "Wikipedia needs a valid language: %s" % valid_language) return True
def changeMode(self): #thermMode schedule / away / hg if self.thermMode not in THERMMODE: raise InvalidParameterException("Invalid Therm mode") headers = self.getAuthorizedHeader() params = {"home_id": self.homeId, "mode": self.thermMode} response = requests.post(_SET_THERM_MODE, headers=headers, params=params)
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.configuration['credentials_file'] is None: raise InvalidParameterException("Google news needs a credentials_file") if self.configuration['client_secret_file'] is None: raise InvalidParameterException("Google news needs a client_secret_file") if self.configuration['max_results'] is None: raise InvalidParameterException("Google news needs a max_results") if self.configuration['application_name'] is None: raise InvalidParameterException("Google news needs a application_name") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.configuration['host'] is None: raise InvalidParameterException("Domoticz host is required") if self.configuration['action'] is None: raise InvalidParameterException("Domoticz action is required") if self.configuration['action'] in ['get_device', 'set_switch'] and self.configuration['device'] is None: raise InvalidParameterException("Domoticz device is required for the action %s" % self.configuration['action']) logger.debug(self.configuration) if self.configuration['action'] in ['set_switch'] and self.configuration['action_value'] is None: raise InvalidParameterException("Domoticz action_value is required for the action %s" % self.configuration['action']) return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron. :return: True if parameters are ok, raise an exception otherwise. .. raises:: MissingParameterException """ if self.configuration['todotxt_file'] is None \ or os.path.isfile(self.configuration['todotxt_file']) is False: raise InvalidParameterException( "Todotxt need a valid and existing todo file") if self.configuration['action'] is None: raise InvalidParameterException("An action is require") if self.configuration[ 'action'] == "add" and self.configuration['content'] is None: raise InvalidParameterException( "A content is required when the action is add") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: InvalidParameterException """ if self.image_path is None: raise InvalidParameterException("Ocr needs an image path") return True
def _is_parameters_ok(self): """ Check that all given parameter are valid :return: True if all given parameter are ok """ if self.state not in ["on", "off"]: raise InvalidParameterException("[Launch_radio] State must be 'on' or 'off'") if self.state == "ok": if self.radio_url is None: raise InvalidParameterException("[Launch_radio] You have to specify a radio_url parameter") if self.is_playable_url(self.radio_url) is not True: raise InvalidParameterException("[Launch_radio] The radio_url parameter you specified is not a valid playable url") if self.radio_name is None: raise InvalidParameterException("[Launch_radio] You have to specify a radio_name parameter") # if wait auto_stop_minutes is set, must be an integer or string convertible to integer if self.auto_stop_minutes is not None: if not isinstance(self.auto_stop_minutes, int): try: self.auto_stop_minutes = int(self.auto_stop_minutes) except ValueError: raise InvalidParameterException("[Launch_radio] auto_stop_minutes must be an integer") # check auto_stop_minutes is positive if self.auto_stop_minutes < 1: raise InvalidParameterException("[Launch_radio] auto_stop_minutes must be set at least to 1 minute") return True
def _is_parameters_ok(self): """ Check if received parameters are ok to perform operations in the neuron :return: true if parameters are ok, raise an exception otherwise .. raises:: MissingParameterException """ if self.configuration['host'] is None and self.configuration[ 'name'] is None: raise MissingParameterException("Host or Name parameter required") if self.configuration['host'] is not None and self.configuration[ 'name'] is not None: raise InvalidParameterException( "Host and Name parameter can not be used simultaneously") if self.configuration['action'] is None: raise InvalidParameterException("Soundtouch needs an action") if self.configuration['volume'] is not None and ( self.configuration['volume'] < 0 or self.configuration['volume'] > 100): raise InvalidParameterException( "Volume must be greater than 0 and lower than 100") elif self.configuration['action'] not in [ 'play', 'stop', 'set_volume', 'mute', 'set_preset' ]: raise InvalidParameterException( "Soundtouch does not support action") elif self.configuration['preset'] is not None and not (isinstance( self.configuration['preset'], int)): raise InvalidParameterException( "Preset must be set to an integer value") elif self.configuration['preset'] <= 0 or self.configuration[ 'preset'] > 6: raise InvalidParameterException( "Preset must be greater than 0 and lower or equal to 6") return True