async def fetch_methods(self, debug): sigs = await self.__class__.fetch_signatures(self.endpoint, self.active_protocol, self.idgen) if debug > 1: _LOGGER.debug("Signatures: %s", sigs) if "error" in sigs: _LOGGER.error("Got error when fetching sigs: %s", sigs["error"]) return None methods = {} for sig in sigs["results"]: name = sig[0] parsed_sig = MethodSignature.from_payload(*sig) if name in methods: _LOGGER.debug( "Got duplicate signature for %s, existing was %s, keeping it.", parsed_sig, methods[name], ) else: methods[name] = Method(self, parsed_sig, debug) self.methods = methods return self.methods
async def from_payload(cls, payload, endpoint, idgen, debug, force_protocol=None): """Create Service object from a payload.""" service_name = payload["service"] if "protocols" not in payload: raise SongpalException( "Unable to find protocols from payload: %s" % payload) protocols = payload["protocols"] _LOGGER.debug("Available protocols for %s: %s", service_name, protocols) if force_protocol and force_protocol.value in protocols: protocol = force_protocol elif "websocket:jsonizer" in protocols: protocol = ProtocolType.WebSocket elif "xhrpost:jsonizer" in protocols: protocol = ProtocolType.XHRPost else: raise SongpalException("No known protocols for %s, got: %s" % (service_name, protocols)) _LOGGER.debug("Using protocol: %s" % protocol) service_endpoint = "%s/%s" % (endpoint, service_name) # creation here we want to pass the created service class to methods. service = cls(service_name, service_endpoint, protocol, idgen, debug) sigs = await cls.fetch_signatures(service_endpoint, protocol, idgen) if debug > 1: _LOGGER.debug("Signatures: %s", sigs) if "error" in sigs: _LOGGER.error("Got error when fetching sigs: %s", sigs["error"]) return None methods = {} for sig in sigs["results"]: name = sig[0] parsed_sig = MethodSignature.from_payload(*sig) if name in methods: _LOGGER.warning( "Got duplicate signature for %s, existing was %s. Keeping the existing one", parsed_sig, methods[name]) else: methods[name] = Method(service, parsed_sig, debug) service.methods = methods if "notifications" in payload and "switchNotifications" in methods: notifications = [ Notification(service_endpoint, methods["switchNotifications"], notification) for notification in payload["notifications"] ] service.notifications = notifications _LOGGER.debug("Got notifications: %s" % notifications) return service
async def from_payload(cls, payload, endpoint, idgen, debug, force_protocol=None): service = payload["service"] methods = {} if 'protocols' not in payload: raise SongpalException( "Unable to find protocols from payload: %s" % payload) protocols = payload['protocols'] _LOGGER.debug("Available protocols for %s: %s", service, protocols) if force_protocol and force_protocol.value in protocols: protocol = force_protocol elif 'websocket:jsonizer' in protocols: protocol = ProtocolType.WebSocket elif 'xhrpost:jsonizer' in protocols: protocol = ProtocolType.XHRPost else: raise SongpalException("No known protocols for %s, got: %s" % (service, protocols)) _LOGGER.debug("Using protocol: %s" % protocol) versions = set() for method in payload['apis']: # TODO we take only the first version here per method # should we prefer the newest version instead of that? if len(method["versions"]) == 0: _LOGGER.warning("No versions found for %s", method) elif len(method["versions"]) > 1: _LOGGER.warning( "More than on version for %s, " "using the first one", method) versions.add(method["versions"][0]["version"]) service_endpoint = "%s/%s" % (endpoint, service) signatures = {} for version in versions: sigs = await cls.fetch_signatures(service_endpoint, version, protocol, idgen) if debug > 1: _LOGGER.debug("Signatures: %s", sigs) if 'error' in sigs: _LOGGER.error("Got error when fetching sigs: %s", sigs['error']) return None for sig in sigs["results"]: signatures[sig[0]] = Signature(*sig) for method in payload["apis"]: name = method["name"] if name in methods: raise SongpalException("Got duplicate %s for %s" % (name, endpoint)) if name not in signatures: _LOGGER.debug("Got no signature for %s on %s" % (name, endpoint)) continue methods[name] = Method(service, service_endpoint, method, signatures[name], protocol, idgen, debug) notifications = [] # TODO switchnotifications check is broken? if "notifications" in payload and "switchNotifications" in methods: notifications = [ Notification(service_endpoint, methods["switchNotifications"], notification) for notification in payload["notifications"] ] return cls(service, methods, notifications, protocols, idgen)