Esempio n. 1
0
def send_entry_not_found(
    connection: websocket_api.ActiveConnection, msg_id: int
) -> None:
    """Send Config entry not found error."""
    connection.send_error(
        msg_id, websocket_api.const.ERR_NOT_FOUND, "Config entry not found"
    )
Esempio n. 2
0
async def ws_get_statistics_during_period(
    opp: OpenPeerPower, connection: websocket_api.ActiveConnection, msg: dict
) -> None:
    """Handle statistics websocket command."""
    start_time_str = msg["start_time"]
    end_time_str = msg.get("end_time")

    start_time = dt_util.parse_datetime(start_time_str)
    if start_time:
        start_time = dt_util.as_utc(start_time)
    else:
        connection.send_error(msg["id"], "invalid_start_time", "Invalid start_time")
        return

    if end_time_str:
        end_time = dt_util.parse_datetime(end_time_str)
        if end_time:
            end_time = dt_util.as_utc(end_time)
        else:
            connection.send_error(msg["id"], "invalid_end_time", "Invalid end_time")
            return
    else:
        end_time = None

    statistics = await opp.async_add_executor_job(
        statistics_during_period,
        opp,
        start_time,
        end_time,
        msg.get("statistic_id"),
    )
    connection.send_result(msg["id"], {"statistics": statistics})
Esempio n. 3
0
    async def ws_delete_item(self, opp: OpenPeerPower,
                             connection: websocket_api.ActiveConnection,
                             msg: dict) -> None:
        """Delete a item."""
        try:
            await self.storage_collection.async_delete_item(
                msg[self.item_id_key])
        except ItemNotFound:
            connection.send_error(
                msg["id"],
                websocket_api.const.ERR_NOT_FOUND,
                f"Unable to find {self.item_id_key} {msg[self.item_id_key]}",
            )

        connection.send_result(msg["id"])
Esempio n. 4
0
 async def ws_create_item(self, opp: OpenPeerPower,
                          connection: websocket_api.ActiveConnection,
                          msg: dict) -> None:
     """Create a item."""
     try:
         data = dict(msg)
         data.pop("id")
         data.pop("type")
         item = await self.storage_collection.async_create_item(data)
         connection.send_result(msg["id"], item)
     except vol.Invalid as err:
         connection.send_error(
             msg["id"],
             websocket_api.const.ERR_INVALID_FORMAT,
             humanize_error(data, err),
         )
     except ValueError as err:
         connection.send_error(msg["id"],
                               websocket_api.const.ERR_INVALID_FORMAT,
                               str(err))
Esempio n. 5
0
async def ws_camera_stream(
    opp: OpenPeerPower, connection: ActiveConnection, msg: dict
) -> None:
    """Handle get camera stream websocket command.

    Async friendly.
    """
    try:
        entity_id = msg["entity_id"]
        camera = _get_camera_from_entity_id(opp, entity_id)
        url = await _async_stream_endpoint_url(opp, camera, fmt=msg["format"])
        connection.send_result(msg["id"], {"url": url})
    except OpenPeerPowerError as ex:
        _LOGGER.error("Error requesting stream: %s", ex)
        connection.send_error(msg["id"], "start_stream_failed", str(ex))
    except asyncio.TimeoutError:
        _LOGGER.error("Timeout getting stream source")
        connection.send_error(
            msg["id"], "start_stream_failed", "Timeout getting stream source"
        )
Esempio n. 6
0
    async def ws_update_item(self, opp: OpenPeerPower,
                             connection: websocket_api.ActiveConnection,
                             msg: dict) -> None:
        """Update a item."""
        data = dict(msg)
        msg_id = data.pop("id")
        item_id = data.pop(self.item_id_key)
        data.pop("type")

        try:
            item = await self.storage_collection.async_update_item(
                item_id, data)
            connection.send_result(msg_id, item)
        except ItemNotFound:
            connection.send_error(
                msg["id"],
                websocket_api.const.ERR_NOT_FOUND,
                f"Unable to find {self.item_id_key} {item_id}",
            )
        except vol.Invalid as err:
            connection.send_error(
                msg["id"],
                websocket_api.const.ERR_INVALID_FORMAT,
                humanize_error(data, err),
            )
        except ValueError as err:
            connection.send_error(msg_id,
                                  websocket_api.const.ERR_INVALID_FORMAT,
                                  str(err))