Fix Trafikverket Camera if no location data (#101463)
This commit is contained in:
parent
6853d54050
commit
8a033ee554
3 changed files with 65 additions and 1 deletions
|
@ -35,6 +35,7 @@ class TVCameraConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
"""Validate input from user input."""
|
||||
errors: dict[str, str] = {}
|
||||
camera_info: CameraInfo | None = None
|
||||
camera_location: str | None = None
|
||||
|
||||
web_session = async_get_clientsession(self.hass)
|
||||
camera_api = TrafikverketCamera(web_session, sensor_api)
|
||||
|
@ -49,7 +50,12 @@ class TVCameraConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
except UnknownError:
|
||||
errors["base"] = "cannot_connect"
|
||||
|
||||
camera_location = camera_info.location if camera_info else None
|
||||
if camera_info:
|
||||
if _location := camera_info.location:
|
||||
camera_location = _location
|
||||
else:
|
||||
camera_location = camera_info.camera_name
|
||||
|
||||
return (errors, camera_location)
|
||||
|
||||
async def async_step_reauth(self, entry_data: Mapping[str, Any]) -> FlowResult:
|
||||
|
|
|
@ -67,3 +67,24 @@ def fixture_get_camera() -> CameraInfo:
|
|||
status="Running",
|
||||
camera_type="Road",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(name="get_camera_no_location")
|
||||
def fixture_get_camera_no_location() -> CameraInfo:
|
||||
"""Construct Camera Mock."""
|
||||
|
||||
return CameraInfo(
|
||||
camera_name="Test Camera",
|
||||
camera_id="1234",
|
||||
active=True,
|
||||
deleted=False,
|
||||
description="Test Camera for testing",
|
||||
direction="180",
|
||||
fullsizephoto=True,
|
||||
location=None,
|
||||
modified=datetime(2022, 4, 4, 4, 4, 4, tzinfo=dt_util.UTC),
|
||||
phototime=datetime(2022, 4, 4, 4, 4, 4, tzinfo=dt_util.UTC),
|
||||
photourl="https://www.testurl.com/test_photo.jpg",
|
||||
status="Running",
|
||||
camera_type="Road",
|
||||
)
|
||||
|
|
|
@ -56,6 +56,43 @@ async def test_form(hass: HomeAssistant, get_camera: CameraInfo) -> None:
|
|||
assert result2["result"].unique_id == "trafikverket_camera-Test location"
|
||||
|
||||
|
||||
async def test_form_no_location_data(
|
||||
hass: HomeAssistant, get_camera_no_location: CameraInfo
|
||||
) -> None:
|
||||
"""Test we get the form."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.trafikverket_camera.config_flow.TrafikverketCamera.async_get_camera",
|
||||
return_value=get_camera_no_location,
|
||||
), patch(
|
||||
"homeassistant.components.trafikverket_camera.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_API_KEY: "1234567890",
|
||||
CONF_LOCATION: "Test Cam",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "Test Camera"
|
||||
assert result2["data"] == {
|
||||
"api_key": "1234567890",
|
||||
"location": "Test Camera",
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
assert result2["result"].unique_id == "trafikverket_camera-Test Camera"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("side_effect", "error_key", "base_error"),
|
||||
[
|
||||
|
|
Loading…
Add table
Reference in a new issue