Ejemplo n.º 1
0
    def process_response(self, json_obj: Dict[str,
                                              Any]) -> PairChallengeResponse:
        """Return response to command to complete pairing process."""
        item = dict_get_case_insensitive(json_obj, ResponseKey.ITEM)

        return PairChallengeResponse(
            dict_get_case_insensitive(item, PairingResponseKey.AUTH_TOKEN))
Ejemplo n.º 2
0
async def async_validate_response(web_response: ClientResponse) -> Dict[str, Any]:
    """Validate response to API command is as expected and return response."""
    if HTTP_OK != web_response.status:
        raise Exception(
            "Device is unreachable? Status code: {0}".format(web_response.status)
        )

    try:
        data = json.loads(await web_response.text())
        _LOGGER.debug("Response: %s", data)
    except Exception:
        raise Exception("Failed to parse response: {0}".format(web_response.content))

    status_obj = dict_get_case_insensitive(data, "status")

    if not status_obj:
        raise Exception("Unknown response")

    result_status = dict_get_case_insensitive(status_obj, "result")

    if result_status and result_status.lower() == STATUS_INVALID_PARAMETER:
        raise Exception("invalid value specified")
    elif not result_status or result_status.lower() != STATUS_SUCCESS:
        raise Exception(
            "unexpected status {0}: {1}".format(
                result_status, dict_get_case_insensitive(status_obj, "detail")
            )
        )

    return data
Ejemplo n.º 3
0
    def process_response(self, json_obj: Dict[str, Any]) -> BeginPairResponse:
        item = dict_get_case_insensitive(json_obj, ResponseKey.ITEM)

        return BeginPairResponse(
            dict_get_case_insensitive(item, PairingResponseKey.CHALLENGE_TYPE),
            dict_get_case_insensitive(item,
                                      PairingResponseKey.PAIRING_REQ_TOKEN),
        )
Ejemplo n.º 4
0
    def process_response(self, json_obj: Dict[str, Any]) -> AppConfig:
        """Return response to command to get currently running app's config."""
        item = dict_get_case_insensitive(json_obj, ResponseKey.ITEM, {})
        current_app_id = dict_get_case_insensitive(item, ResponseKey.VALUE)

        if current_app_id:
            return AppConfig(**current_app_id)

        return AppConfig()
Ejemplo n.º 5
0
    def __init__(self, json_obj: Dict[str, Any]) -> None:
        self.id = None
        id = dict_get_case_insensitive(json_obj, ResponseKey.HASHVAL)
        if id:
            self.id = int(id)

        self.c_name = dict_get_case_insensitive(json_obj, ResponseKey.CNAME)
        self.type = dict_get_case_insensitive(json_obj, ResponseKey.TYPE)
        self.name = dict_get_case_insensitive(json_obj, ResponseKey.NAME)
        self.value = dict_get_case_insensitive(json_obj, ResponseKey.VALUE)
Ejemplo n.º 6
0
    def __init__(self, json_obj: Dict[str, Any]) -> None:
        """Initialize individual item setting."""
        self.id = None
        id = dict_get_case_insensitive(json_obj, ResponseKey.HASHVAL)
        if id is not None:
            self.id = int(id)

        self.c_name = dict_get_case_insensitive(json_obj, ResponseKey.CNAME)
        self.type = dict_get_case_insensitive(json_obj, ResponseKey.TYPE)
        self.name = dict_get_case_insensitive(json_obj, ResponseKey.NAME)
        self.value = dict_get_case_insensitive(json_obj, ResponseKey.VALUE)

        self.min = None
        min = dict_get_case_insensitive(json_obj, ResponseKey.MINIMUM)
        if min is not None:
            self.min = int(min)

        self.max = None
        max = dict_get_case_insensitive(json_obj, ResponseKey.MAXIMUM)
        if max is not None:
            self.max = int(max)

        self.center = None
        center = dict_get_case_insensitive(json_obj, ResponseKey.CENTER)
        if center is not None:
            self.center = int(center)

        self.choices = dict_get_case_insensitive(json_obj, ResponseKey.ELEMENTS, [])
Ejemplo n.º 7
0
    def process_response(self, json_obj: Dict[str, Any]) -> Optional[List[InputItem]]:
        items = dict_get_case_insensitive(json_obj, ResponseKey.ITEMS)

        if items:
            return [
                InputItem(itm, True)
                for itm in items
                if dict_get_case_insensitive(itm, ResponseKey.CNAME) != "current_input"
            ]

        return None
Ejemplo n.º 8
0
 def process_response(self, json_obj: Dict[str, Any]) -> Any:
     items = [
         Item(item) for item in dict_get_case_insensitive(
             json_obj, ResponseKey.ITEMS, [])
     ]
     return [
         item.c_name for item in items
         if item.type.lower() in (TYPE_EQ_LIST, TYPE_EQ_SLIDER)
     ]
Ejemplo n.º 9
0
    def process_response(self, json_obj: Dict[str, Any]) -> Optional[InputItem]:
        items = dict_get_case_insensitive(json_obj, ResponseKey.ITEMS)

        v_input = None

        if items:
            v_input = InputItem(items[0], False)

        return v_input
Ejemplo n.º 10
0
    def process_response(self, json_obj: Dict[str,
                                              Any]) -> Dict[str, List[str]]:
        """Return response to command to get list of all setting names and corresponding options for settings of type XList."""
        items = [
            Item(item) for item in dict_get_case_insensitive(
                json_obj, ResponseKey.ITEMS, [])
            if item.type.lower() == TYPE_X_LIST
        ]

        return {item.c_name: item.choices for item in items}
Ejemplo n.º 11
0
 def process_response(self, json_obj: Dict[str, Any]) -> Optional[str]:
     """Return response to command to get device model name."""
     return get_value_from_path(
         dict_get_case_insensitive(
             super(GetModelNameCommand, self).process_response(json_obj),
             ResponseKey.VALUE,
             {},
         ),
         self.paths,
     )
Ejemplo n.º 12
0
 def process_response(
         self, json_obj: Dict[str, Any]) -> Dict[str, Union[int, str]]:
     items = [
         Item(item) for item in dict_get_case_insensitive(
             json_obj, ResponseKey.ITEMS, [])
     ]
     audio_settings = {}
     for item in items:
         if item.type.lower() in (TYPE_EQ_LIST, TYPE_EQ_SLIDER):
             audio_settings[item.c_name] = item.value
     return audio_settings
Ejemplo n.º 13
0
 def process_response(self, json_obj: Dict[str, Any]) -> List[str]:
     """Return response to command to get list of all setting types."""
     items = [
         Item(item) for item in dict_get_case_insensitive(
             json_obj, ResponseKey.ITEMS, [])
     ]
     return [
         item.c_name for item in items
         if item.type.lower() == TYPE_MENU and item.c_name not in (
             "cast", "input", "devices", "network")
     ]
Ejemplo n.º 14
0
    def __init__(self, json_item: Dict[str, Any],
                 is_extended_metadata: bool) -> None:
        """Initialize input device."""
        super(InputItem, self).__init__(json_item)
        self.meta_name = None
        self.meta_data = None

        meta = dict_get_case_insensitive(json_item, ResponseKey.VALUE)

        if meta:
            if is_extended_metadata:
                self.meta_name = dict_get_case_insensitive(
                    meta, ResponseKey.NAME)
                self.meta_data = dict_get_case_insensitive(
                    meta, ResponseKey.METADATA)
            else:
                self.meta_name = meta

        if not self.meta_name:
            self.meta_name = self.c_name
Ejemplo n.º 15
0
    def process_response(self, json_obj: Dict[str,
                                              Any]) -> Optional[InputItem]:
        """Return response to command to get currently active input."""
        items = dict_get_case_insensitive(json_obj, ResponseKey.ITEMS)

        v_input = None

        if items:
            v_input = InputItem(items[0], False)

        return v_input
Ejemplo n.º 16
0
 def process_response(
         self, json_obj: Dict[str, Any]) -> Dict[str, Union[int, str]]:
     """Return response to command to get list of all setting names and corresponding values."""
     items = [
         Item(item) for item in dict_get_case_insensitive(
             json_obj, ResponseKey.ITEMS, [])
     ]
     return {
         item.c_name: item.value
         for item in items
         if item.type.lower() in (TYPE_LIST, TYPE_SLIDER, TYPE_VALUE)
     }
Ejemplo n.º 17
0
async def async_validate_response(
        web_response: ClientResponse) -> Dict[str, Any]:
    if HTTP_OK != web_response.status:
        raise Exception("Device is unreachable? Status code: {0}".format(
            web_response.status))

    try:
        data = json.loads(await web_response.text())
    except Exception:
        raise Exception("Failed to parse response: {0}".format(
            web_response.content))

    status_obj = dict_get_case_insensitive(data, "status")

    if not status_obj:
        raise Exception("Unknown response")

    result_status = dict_get_case_insensitive(status_obj, "result")

    if not result_status or result_status.lower() != STATUS_SUCCESS:
        raise Exception("Unexpected response {0}: {1}".format(
            result_status, dict_get_case_insensitive(status_obj, "detail")))

    return data
Ejemplo n.º 18
0
    def process_response(self, json_obj: Dict[str, Any]) -> Any:
        items = [
            Item(item) for item in dict_get_case_insensitive(
                json_obj, ResponseKey.ITEMS, [])
        ]

        for itm in items:
            if itm.c_name.lower() in (
                    ITEM_CNAME.get(self.item_name, ""),
                    self.item_name,
            ):
                if itm.value is not None:
                    return itm

        if self.default_return is not None:
            return DefaultReturnItem(self.default_return)

        return None
Ejemplo n.º 19
0
    def process_response(self, json_obj: Dict[str, Any]) -> Any:
        """Return response to command to get individual item setting."""
        items = [
            Item(item) for item in dict_get_case_insensitive(
                json_obj, ResponseKey.ITEMS, [])
        ]

        for itm in items:
            if itm.c_name.lower() in (
                    ITEM_CNAME.get(self.item_name, ""),
                    self.item_name,
            ) and (itm.value is not None or itm.center is not None
                   or itm.choices is not None):
                return itm

        if self.default_return is not None:
            return DefaultReturnItem(self.default_return)

        return None
Ejemplo n.º 20
0
    def __init__(self, json_item: Dict[str, Any], is_extended_metadata: bool) -> None:
        self.id = int(dict_get_case_insensitive(json_item, ResponseKey.HASHVAL))
        self.c_name = dict_get_case_insensitive(json_item, ResponseKey.CNAME)
        self.type = dict_get_case_insensitive(json_item, ResponseKey.TYPE)
        self.name = dict_get_case_insensitive(json_item, ResponseKey.NAME)
        self.meta_name = None
        self.meta_data = None

        meta = dict_get_case_insensitive(json_item, ResponseKey.VALUE)

        if meta:
            if is_extended_metadata:
                self.meta_name = dict_get_case_insensitive(meta, ResponseKey.NAME)
                self.meta_data = dict_get_case_insensitive(meta, ResponseKey.METADATA)
            else:
                self.meta_name = meta

        if not self.meta_name:
            self.meta_name = self.c_name
Ejemplo n.º 21
0
    def process_response(
        self, json_obj: Dict[str, Any]
    ) -> Dict[str, Union[List[str], Dict[str, Union[int, str]]]]:
        """Return response to command to get list of all setting names and corresponding options."""
        items = [
            Item(item) for item in dict_get_case_insensitive(
                json_obj, ResponseKey.ITEMS, [])
        ]

        settings_options = {}
        for item in items:
            if item.type.lower() in (TYPE_SLIDER, TYPE_VALUE):
                settings_options[item.c_name] = {
                    "min": item.min,
                    "max": item.max
                }
                if item.center is not None:
                    settings_options[item.c_name].update(
                        {"default": item.center})
            elif item.type.lower() == TYPE_LIST:
                settings_options[item.c_name] = item.choices.copy()

        return settings_options
Ejemplo n.º 22
0
 def process_response(self, json_obj: Dict[str, Any]) -> bool:
     items = dict_get_case_insensitive(json_obj, ResponseKey.ITEMS)
     return get_value_from_path(
         dict_get_case_insensitive(items[0], ResponseKey.VALUE, {}),
         self.paths)
Ejemplo n.º 23
0
    def process_response(self, json_obj: Dict[str,
                                              Any]) -> PairChallengeResponse:
        item = dict_get_case_insensitive(json_obj, ResponseKey.ITEM)

        return PairChallengeResponse(
            dict_get_case_insensitive(item, PairingResponseKey.AUTH_TOKEN))
Ejemplo n.º 24
0
 def process_response(self, json_obj: Dict[str, Any]) -> Dict[str, Any]:
     """Return response to command to get device info."""
     return dict_get_case_insensitive(json_obj, ResponseKey.ITEMS, [{}])[0]