diff --git a/homeassistant/components/trafikverket_camera/config_flow.py b/homeassistant/components/trafikverket_camera/config_flow.py index b8a14a5424e..e1f8220c4ff 100644 --- a/homeassistant/components/trafikverket_camera/config_flow.py +++ b/homeassistant/components/trafikverket_camera/config_flow.py @@ -10,7 +10,7 @@ from pytrafikverket.exceptions import ( NoCameraFound, UnknownError, ) -from pytrafikverket.trafikverket_camera import TrafikverketCamera +from pytrafikverket.trafikverket_camera import CameraInfo, TrafikverketCamera import voluptuous as vol from homeassistant import config_entries @@ -29,14 +29,17 @@ class TVCameraConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): entry: config_entries.ConfigEntry | None - async def validate_input(self, sensor_api: str, location: str) -> dict[str, str]: + async def validate_input( + self, sensor_api: str, location: str + ) -> tuple[dict[str, str], str | None]: """Validate input from user input.""" errors: dict[str, str] = {} + camera_info: CameraInfo | None = None web_session = async_get_clientsession(self.hass) camera_api = TrafikverketCamera(web_session, sensor_api) try: - await camera_api.async_get_camera(location) + camera_info = await camera_api.async_get_camera(location) except NoCameraFound: errors["location"] = "invalid_location" except MultipleCamerasFound: @@ -46,7 +49,8 @@ class TVCameraConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): except UnknownError: errors["base"] = "cannot_connect" - return errors + camera_location = camera_info.location if camera_info else None + return (errors, camera_location) async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult: """Handle re-authentication with Trafikverket.""" @@ -58,13 +62,15 @@ class TVCameraConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Confirm re-authentication with Trafikverket.""" - errors = {} + errors: dict[str, str] = {} if user_input: api_key = user_input[CONF_API_KEY] assert self.entry is not None - errors = await self.validate_input(api_key, self.entry.data[CONF_LOCATION]) + errors, _ = await self.validate_input( + api_key, self.entry.data[CONF_LOCATION] + ) if not errors: self.hass.config_entries.async_update_entry( @@ -91,22 +97,23 @@ class TVCameraConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self, user_input: dict[str, str] | None = None ) -> FlowResult: """Handle the initial step.""" - errors = {} + errors: dict[str, str] = {} if user_input: api_key = user_input[CONF_API_KEY] location = user_input[CONF_LOCATION] - errors = await self.validate_input(api_key, location) + errors, camera_location = await self.validate_input(api_key, location) if not errors: - await self.async_set_unique_id(f"{DOMAIN}-{location}") + assert camera_location + await self.async_set_unique_id(f"{DOMAIN}-{camera_location}") self._abort_if_unique_id_configured() return self.async_create_entry( - title=user_input[CONF_LOCATION], + title=camera_location, data={ CONF_API_KEY: api_key, - CONF_LOCATION: location, + CONF_LOCATION: camera_location, }, ) diff --git a/homeassistant/components/trafikverket_camera/manifest.json b/homeassistant/components/trafikverket_camera/manifest.json index 440d7237171..d23631c6878 100644 --- a/homeassistant/components/trafikverket_camera/manifest.json +++ b/homeassistant/components/trafikverket_camera/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/trafikverket_camera", "iot_class": "cloud_polling", "loggers": ["pytrafikverket"], - "requirements": ["pytrafikverket==0.3.5"] + "requirements": ["pytrafikverket==0.3.6"] } diff --git a/homeassistant/components/trafikverket_ferry/manifest.json b/homeassistant/components/trafikverket_ferry/manifest.json index 47f1e62be00..9d0b904290c 100644 --- a/homeassistant/components/trafikverket_ferry/manifest.json +++ b/homeassistant/components/trafikverket_ferry/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/trafikverket_ferry", "iot_class": "cloud_polling", "loggers": ["pytrafikverket"], - "requirements": ["pytrafikverket==0.3.5"] + "requirements": ["pytrafikverket==0.3.6"] } diff --git a/homeassistant/components/trafikverket_train/manifest.json b/homeassistant/components/trafikverket_train/manifest.json index 47b4c21c867..ab1f7feb3f7 100644 --- a/homeassistant/components/trafikverket_train/manifest.json +++ b/homeassistant/components/trafikverket_train/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/trafikverket_train", "iot_class": "cloud_polling", "loggers": ["pytrafikverket"], - "requirements": ["pytrafikverket==0.3.5"] + "requirements": ["pytrafikverket==0.3.6"] } diff --git a/homeassistant/components/trafikverket_weatherstation/manifest.json b/homeassistant/components/trafikverket_weatherstation/manifest.json index 8c46afa5972..138af544066 100644 --- a/homeassistant/components/trafikverket_weatherstation/manifest.json +++ b/homeassistant/components/trafikverket_weatherstation/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/trafikverket_weatherstation", "iot_class": "cloud_polling", "loggers": ["pytrafikverket"], - "requirements": ["pytrafikverket==0.3.5"] + "requirements": ["pytrafikverket==0.3.6"] } diff --git a/requirements_all.txt b/requirements_all.txt index 5341de900da..f6929065d3c 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2199,7 +2199,7 @@ pytradfri[async]==9.0.1 # homeassistant.components.trafikverket_ferry # homeassistant.components.trafikverket_train # homeassistant.components.trafikverket_weatherstation -pytrafikverket==0.3.5 +pytrafikverket==0.3.6 # homeassistant.components.usb pyudev==0.23.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index da11cbb5775..c22770d9d2f 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1619,7 +1619,7 @@ pytradfri[async]==9.0.1 # homeassistant.components.trafikverket_ferry # homeassistant.components.trafikverket_train # homeassistant.components.trafikverket_weatherstation -pytrafikverket==0.3.5 +pytrafikverket==0.3.6 # homeassistant.components.usb pyudev==0.23.2 diff --git a/tests/components/trafikverket_camera/test_config_flow.py b/tests/components/trafikverket_camera/test_config_flow.py index 38c49d54208..aa6122b7efe 100644 --- a/tests/components/trafikverket_camera/test_config_flow.py +++ b/tests/components/trafikverket_camera/test_config_flow.py @@ -10,6 +10,7 @@ from pytrafikverket.exceptions import ( NoCameraFound, UnknownError, ) +from pytrafikverket.trafikverket_camera import CameraInfo from homeassistant import config_entries from homeassistant.components.trafikverket_camera.const import CONF_LOCATION, DOMAIN @@ -20,7 +21,7 @@ from homeassistant.data_entry_flow import FlowResultType from tests.common import MockConfigEntry -async def test_form(hass: HomeAssistant) -> None: +async def test_form(hass: HomeAssistant, get_camera: CameraInfo) -> None: """Test we get the form.""" result = await hass.config_entries.flow.async_init( @@ -31,6 +32,7 @@ async def test_form(hass: HomeAssistant) -> None: with patch( "homeassistant.components.trafikverket_camera.config_flow.TrafikverketCamera.async_get_camera", + return_value=get_camera, ), patch( "homeassistant.components.trafikverket_camera.async_setup_entry", return_value=True, @@ -39,7 +41,7 @@ async def test_form(hass: HomeAssistant) -> None: result["flow_id"], { CONF_API_KEY: "1234567890", - CONF_LOCATION: "Test location", + CONF_LOCATION: "Test loc", }, ) await hass.async_block_till_done()