Ejemplo n.º 1
0
    async def async_post_auth(self, request: web.Request, request_data: dict):
        """Handle requests to create a username for the emulated hue bridge."""
        if "devicetype" not in request_data:
            LOGGER.warning("devicetype not specified")
            # custom error message
            return send_error_response(request.path,
                                       "devicetype not specified", 302)
        if not self.config.link_mode_enabled:
            await self.config.async_enable_link_mode_discovery()
            return send_error_response(request.path, "link button not pressed",
                                       101)

        userdetails = await self.config.async_create_user(
            request_data["devicetype"])
        response = [{"success": {"username": userdetails["username"]}}]
        if request_data.get("generateclientkey"):
            response[0]["success"]["clientkey"] = userdetails["clientkey"]
        LOGGER.info("Client %s registered", userdetails["name"])
        return send_json_response(response)
Ejemplo n.º 2
0
 async def async_update_light(self, request: web.Request,
                              request_data: dict):
     """Handle requests to update a light."""
     light_id = request.match_info["light_id"]
     username = request.match_info["username"]
     light_conf = await self.config.async_get_storage_value(
         "lights", light_id)
     if not light_conf:
         return send_error_response(request.path, "no light config", 404)
     update_dict(light_conf, request_data)
     return send_success_response(request.path, request_data, username)
Ejemplo n.º 3
0
 async def async_unknown_request(self, request: web.Request):
     """Handle unknown requests (catch-all)."""
     if request.method in ["PUT", "POST"]:
         try:
             request_data = await request.json()
         except json.decoder.JSONDecodeError:
             request_data = await request.text()
         LOGGER.warning("Invalid/unknown request: %s --> %s", request,
                        request_data)
     else:
         LOGGER.warning("Invalid/unknown request: %s", request)
     return send_error_response(request.path, "unknown request", 404)
Ejemplo n.º 4
0
 async def async_update_localitem(self, request: web.Request,
                                  request_data: dict):
     """Handle requests to update an item in localstorage."""
     item_id = request.match_info["item_id"]
     itemtype = request.match_info["itemtype"]
     username = request.match_info["username"]
     local_item = await self.config.async_get_storage_value(
         itemtype, item_id)
     if not local_item:
         return send_error_response(request.path, "no localitem", 404)
     update_dict(local_item, request_data)
     await self.config.async_set_storage_value(itemtype, item_id,
                                               local_item)
     return send_success_response(request.path, request_data, username)
Ejemplo n.º 5
0
    async def async_update_group(self, request: web.Request,
                                 request_data: dict):
        """Handle requests to update a group."""
        group_id = request.match_info["group_id"]
        username = request.match_info["username"]
        group_conf = await self.config.async_get_storage_value(
            "groups", group_id)
        if not group_conf:
            return send_error_response(request.path, "no group config", 404)
        update_dict(group_conf, request_data)

        # Hue entertainment support (experimental)
        if "stream" in group_conf:
            if group_conf["stream"].get("active"):
                # Requested streaming start
                LOGGER.debug(
                    "Start Entertainment mode for group %s - params: %s",
                    group_id,
                    request_data,
                )
                del group_conf["stream"]["active"]
                if not self.streaming_api:
                    user_data = await self.config.async_get_user(username)
                    self.streaming_api = EntertainmentAPI(
                        self.hue, group_conf, user_data)
                group_conf["stream"]["owner"] = username
                if not group_conf["stream"].get("proxymode"):
                    group_conf["stream"]["proxymode"] = "auto"
                if not group_conf["stream"].get("proxynode"):
                    group_conf["stream"]["proxynode"] = "/bridge"
            else:
                # Request streaming stop
                LOGGER.info(
                    "Stop Entertainment mode for group %s - params: %s",
                    group_id,
                    request_data,
                )
                if self.streaming_api:
                    # stop service if needed
                    self.streaming_api.stop()
                    self.streaming_api = None

        await self.config.async_set_storage_value("groups", group_id,
                                                  group_conf)
        return send_success_response(request.path, request_data, username)
Ejemplo n.º 6
0
 async def wrapped_func(cls, request: web.Request):
     if log_request:
         LOGGER.debug("%s %s", request.method, request.path)
     # check username
     if check_user:
         username = request.match_info.get("username")
         if not username or not await cls.config.async_get_user(username
                                                                ):
             return send_error_response(request.path,
                                        "unauthorized user", 1)
     # check and unpack (json) body if needed
     if request.method in ["PUT", "POST"]:
         try:
             request_data = await request.json()
         except ValueError:
             request_data = await request.text()
         LOGGER.debug(request_data)
         return await func(cls, request, request_data)
     return await func(cls, request)