Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store generic camera flow data in flow handler attributes #127405

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions homeassistant/components/generic/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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
Expand Down Expand Up @@ -47,7 +47,6 @@
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
Expand Down Expand Up @@ -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 = ""

Expand Down Expand Up @@ -370,7 +370,7 @@ async def async_step_user(
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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -443,7 +444,7 @@ async def async_step_init(
}
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",
Expand Down Expand Up @@ -494,15 +495,17 @@ def __init__(self, hass: HomeAssistant) -> None:
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")
Expand Down