Store generic camera flow data in flow handler attributes (#127405)

This commit is contained in:
Erik Montnemery 2024-10-03 12:23:40 +02:00 committed by GitHub
parent 14c2778558
commit 2b50f65c49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,7 +9,7 @@ from datetime import datetime
from errno import EHOSTUNREACH, EIO
import io
import logging
from typing import Any
from typing import Any, cast
from aiohttp import web
from httpx import HTTPStatusError, RequestError, TimeoutException
@ -47,7 +47,6 @@ from homeassistant.const import (
HTTP_DIGEST_AUTHENTICATION,
)
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import UnknownFlow
from homeassistant.exceptions import HomeAssistantError, TemplateError
from homeassistant.helpers import config_validation as cv, template as template_helper
from homeassistant.helpers.httpx_client import get_async_client
@ -316,6 +315,7 @@ class GenericIPCamConfigFlow(ConfigFlow, domain=DOMAIN):
def __init__(self) -> None:
"""Initialize Generic ConfigFlow."""
self.preview_cam: dict[str, Any] = {}
self.user_input: dict[str, Any] = {}
self.title = ""
@ -370,7 +370,7 @@ class GenericIPCamConfigFlow(ConfigFlow, domain=DOMAIN):
title=self.title, data={}, options=self.user_input
)
# temporary preview for user to check the image
self.context["preview_cam"] = user_input
self.preview_cam = user_input
return await self.async_step_user_confirm_still()
elif self.user_input:
user_input = self.user_input
@ -412,6 +412,7 @@ class GenericOptionsFlowHandler(OptionsFlow):
def __init__(self, config_entry: ConfigEntry) -> None:
"""Initialize Generic IP Camera options flow."""
self.config_entry = config_entry
self.preview_cam: dict[str, Any] = {}
self.user_input: dict[str, Any] = {}
async def async_step_init(
@ -443,7 +444,7 @@ class GenericOptionsFlowHandler(OptionsFlow):
}
self.user_input = data
# temporary preview for user to check the image
self.context["preview_cam"] = data
self.preview_cam = data
return await self.async_step_confirm_still()
return self.async_show_form(
step_id="init",
@ -494,15 +495,17 @@ class CameraImagePreview(HomeAssistantView):
async def get(self, request: web.Request, flow_id: str) -> web.Response:
"""Start a GET request."""
_LOGGER.debug("processing GET request for flow_id=%s", flow_id)
try:
flow = self.hass.config_entries.flow.async_get(flow_id)
except UnknownFlow:
try:
flow = self.hass.config_entries.options.async_get(flow_id)
except UnknownFlow as exc:
_LOGGER.warning("Unknown flow while getting image preview")
raise web.HTTPNotFound from exc
user_input = flow["context"]["preview_cam"]
flow = cast(
GenericIPCamConfigFlow,
self.hass.config_entries.flow._progress.get(flow_id), # noqa: SLF001
) or cast(
GenericOptionsFlowHandler,
self.hass.config_entries.options._progress.get(flow_id), # noqa: SLF001
)
if not flow:
_LOGGER.warning("Unknown flow while getting image preview")
raise web.HTTPNotFound
user_input = flow.preview_cam
camera = GenericCamera(self.hass, user_input, flow_id, "preview")
if not camera.is_on:
_LOGGER.debug("Camera is off")