def delete_system_variable(self, name): """Delete a system variable from CCU / Homegear.""" if self.backend == BACKEND_CCU and self.username and self.password: LOG.debug( "delete_system_variable: Getting System variable via JSON-RPC") self.json_rpc_renew() if not self.session: return try: params = {"_session_id_": self.session, ATTR_NAME: name} response = json_rpc_post(self.host, self.json_port, "SysVar.deleteSysVarByName", params, tls=self.json_tls, verify_tls=self.verify_tls) if response[ATTR_ERROR] is None and response[ATTR_RESULT]: deleted = response[ATTR_RESULT] LOG.warning("delete_system_variable: Deleted: %s", str(deleted)) except Exception: LOG.exception("delete_system_variable: Exception") else: try: return self.proxy.deleteSystemVariable(name) except Exception: LOG.exception("delete_system_variable: Exception")
def get_system_variable(self, name): """Get single system variable from CCU / Homegear.""" var = None if self.backend == BACKEND_CCU and self.username and self.password: LOG.debug( "get_system_variable: Getting System variable via JSON-RPC") self.json_rpc_renew() if not self.session: return var try: params = {"_session_id_": self.session, ATTR_NAME: name} response = json_rpc_post(self.host, self.json_port, "SysVar.getValueByName", params, tls=self.json_tls, verify_tls=self.verify_tls) if response[ATTR_ERROR] is None and response[ATTR_RESULT]: # TODO: This does not yet support strings try: var = float(response[ATTR_RESULT]) except Exception: var = response[ATTR_RESULT] == 'true' except Exception: LOG.exception("get_system_variable: Exception") else: try: var = self.proxy.getSystemVariable(name) except Exception: LOG.exception("get_system_variable: Exception") return var
def get_all_system_variables(self): """Get all system variables from CCU / Homegear.""" variables = {} if self.backend == BACKEND_CCU and self.username and self.password: LOG.debug( "get_all_system_variables: Getting all System variables via JSON-RPC" ) self.json_rpc_renew() if not self.session: return variables try: params = {"_session_id_": self.session} response = json_rpc_post(self.host, self.json_port, "SysVar.getAll", params, tls=self.json_tls, verify_tls=self.verify_tls) if response[ATTR_ERROR] is None and response[ATTR_RESULT]: for var in response[ATTR_RESULT]: key, value = parse_ccu_sys_var(var) variables[key] = value except Exception: LOG.exception("get_all_system_variables: Exception") else: try: variables = self.proxy.getAllSystemVariables() except Exception: LOG.exception("get_all_system_variables: Exception") return variables
def set_system_variable(self, name, value): """Set a system variable on CCU / Homegear.""" if self.backend == BACKEND_CCU and self.username and self.password: LOG.debug( "set_system_variable: Setting System variable via JSON-RPC") self.json_rpc_renew() if not self.session: return try: params = { "_session_id_": self.session, ATTR_NAME: name, ATTR_VALUE: value } if value is True or value is False: params[ATTR_VALUE] = int(value) response = json_rpc_post(self.host, self.json_port, "SysVar.setBool", params) else: response = json_rpc_post(self.host, self.json_port, "SysVar.setFloat", params) if response[ATTR_ERROR] is None and response[ATTR_RESULT]: res = response[ATTR_RESULT] LOG.debug( "set_system_variable: Result while setting variable: %s", str(res)) else: if response[ATTR_ERROR]: LOG.debug( "set_system_variable: Error while setting variable: %s", str(response[ATTR_ERROR])) except Exception: LOG.exception("set_system_variable: Exception") else: try: return self.proxy.setSystemVariable(name, value) except Exception: LOG.exception("set_system_variable: Exception")
def json_rpc_renew(self): """Renew JSON-RPC session or perform login.""" if not self.session: self.json_rpc_login() return try: response = json_rpc_post(self.host, self.json_port, "Session.renew", {ATTR_SESSION_ID: self.session}, tls=self.json_tls, verify_tls=self.verify_tls) if response[ATTR_ERROR] is None and response[ATTR_RESULT]: self.session = response[ATTR_RESULT] return self.json_rpc_login() except Exception: LOG.exception( "json_rpc_renew: Exception while renewing JSON-RPC session.")
def json_rpc_logout(self): """Logout of CCU.""" if not self.session: LOG.warning("json_rpc_logout: Not logged in. Not logging out.") return try: params = {"_session_id_": self.session} response = json_rpc_post(self.host, self.json_port, "Session.logout", params, tls=self.json_tls, verify_tls=self.verify_tls) if response[ATTR_ERROR]: LOG.warning("json_rpc_logout: Logout error: %s", response[ATTR_RESULT]) # pylint: disable=broad-except except Exception: LOG.exception( "json_rpc_logout: Exception while logging in via JSON-RPC") return
def json_rpc_login(self): """Login to CCU and return session.""" self.session = False try: params = { ATTR_USERNAME: self.username, ATTR_PASSWORD: self.password, } response = json_rpc_post(self.host, self.json_port, "Session.login", params, tls=self.json_tls, verify_tls=self.verify_tls) if response[ATTR_ERROR] is None and response[ATTR_RESULT]: self.session = response[ATTR_RESULT] if not self.session: LOG.warning("json_rpc_login: Unable to open session: %s", response[ATTR_ERROR]) # pylint: disable=broad-except except Exception: LOG.exception( "json_rpc_login: Exception while logging in via JSON-RPC")
def fetch_names_json(self): """ Get all names via JSON-RPS and store in data.NAMES. """ if not self.backend == BACKEND_CCU: LOG.warning( "fetch_names_json: No CCU detected. Not fetching names via JSON-RPC." ) return if not self.username: LOG.warning( "fetch_names_json: No username set. Not fetching names via JSON-RPC." ) return LOG.debug("fetch_names_json: Fetching names via JSON-RPC.") try: self.json_rpc_renew() if not self.session: LOG.warning( "fetch_names_json: Login failed. Not fetching names via JSON-RPC." ) return params = {ATTR_SESSION_ID: self.session} response = json_rpc_post(self.host, self.json_port, "Interface.listInterfaces", params, tls=self.json_tls, verify_tls=self.verify_tls) interface = False if response[ATTR_ERROR] is None and response[ATTR_RESULT]: for i in response[ATTR_RESULT]: if i[ATTR_PORT] in [self.port, self.port + 40000]: interface = i[ATTR_NAME] break LOG.debug("fetch_names_json: Got interface: %s", interface) if not interface: return params = {ATTR_SESSION_ID: self.session} response = json_rpc_post(self.host, self.json_port, "Device.listAllDetail", params, tls=self.json_tls, verify_tls=self.verify_tls) if response[ATTR_ERROR] is None and response[ATTR_RESULT]: LOG.debug("fetch_names_json: Resolving devicenames") for device in response[ATTR_RESULT]: if device[ATTR_INTERFACE] != interface: continue try: data.NAMES[self.id][ device[ATTR_ADDRESS]] = device[ATTR_NAME] for channel in device.get(ATTR_CHANNELS, []): data.NAMES[self.id][ channel[ATTR_ADDRESS]] = channel[ATTR_NAME] except Exception: LOG.exception("fetch_names_json: Exception") except Exception: LOG.exception("fetch_names_json: General exception")