"""Test the Scrape config flow.""" from __future__ import annotations from unittest.mock import AsyncMock, Mock, patch import pytest from homeassistant import config_entries from homeassistant.components.local_file.const import DEFAULT_NAME, DOMAIN from homeassistant.const import CONF_FILE_PATH, CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from tests.common import MockConfigEntry async def test_form_sensor(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: """Test we get the form for sensor.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["step_id"] == "user" assert result["type"] is FlowResultType.FORM with ( patch("os.path.isfile", Mock(return_value=True)), patch("os.access", Mock(return_value=True)), patch( "homeassistant.components.local_file.camera.mimetypes.guess_type", Mock(return_value=(None, None)), ), ): result = await hass.config_entries.flow.async_configure( result["flow_id"], { CONF_NAME: DEFAULT_NAME, CONF_FILE_PATH: "mock.file", }, ) await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY assert result["version"] == 1 assert result["options"] == { CONF_NAME: DEFAULT_NAME, CONF_FILE_PATH: "mock.file", } assert len(mock_setup_entry.mock_calls) == 1 async def test_options_flow(hass: HomeAssistant, loaded_entry: MockConfigEntry) -> None: """Test options flow.""" result = await hass.config_entries.options.async_init(loaded_entry.entry_id) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "init" with ( patch("os.path.isfile", Mock(return_value=True)), patch("os.access", Mock(return_value=True)), patch( "homeassistant.components.local_file.camera.mimetypes.guess_type", Mock(return_value=(None, None)), ), ): result = await hass.config_entries.options.async_configure( result["flow_id"], user_input={CONF_FILE_PATH: "mock.new.file"}, ) await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY assert result["data"] == {CONF_NAME: DEFAULT_NAME, CONF_FILE_PATH: "mock.new.file"} await hass.async_block_till_done() # Check the entity was updated, no new entity was created assert len(hass.states.async_all()) == 1 state = hass.states.get("camera.local_file") assert state is not None async def test_validation_options( hass: HomeAssistant, mock_setup_entry: AsyncMock ) -> None: """Test validation.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["step_id"] == "user" assert result["type"] is FlowResultType.FORM with ( patch("os.path.isfile", Mock(return_value=True)), patch("os.access", Mock(return_value=False)), patch( "homeassistant.components.local_file.camera.mimetypes.guess_type", Mock(return_value=(None, None)), ), ): result = await hass.config_entries.flow.async_configure( result["flow_id"], { CONF_NAME: DEFAULT_NAME, CONF_FILE_PATH: "mock.file", }, ) await hass.async_block_till_done() assert result["step_id"] == "user" assert result["type"] is FlowResultType.FORM assert result["errors"] == {"base": "not_readable_path"} with ( patch("os.path.isfile", Mock(return_value=True)), patch("os.access", Mock(return_value=True)), patch( "homeassistant.components.local_file.camera.mimetypes.guess_type", Mock(return_value=(None, None)), ), ): result = await hass.config_entries.flow.async_configure( result["flow_id"], { CONF_NAME: DEFAULT_NAME, CONF_FILE_PATH: "mock.new.file", }, ) await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY assert result["version"] == 1 assert result["options"] == { CONF_NAME: DEFAULT_NAME, CONF_FILE_PATH: "mock.new.file", } assert len(mock_setup_entry.mock_calls) == 1 @pytest.mark.usefixtures("mock_setup_entry") async def test_entry_already_exist( hass: HomeAssistant, loaded_entry: MockConfigEntry ) -> None: """Test abort when entry already exist.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["step_id"] == "user" assert result["type"] is FlowResultType.FORM with ( patch("os.path.isfile", Mock(return_value=True)), patch("os.access", Mock(return_value=True)), patch( "homeassistant.components.local_file.camera.mimetypes.guess_type", Mock(return_value=(None, None)), ), ): result = await hass.config_entries.flow.async_configure( result["flow_id"], { CONF_NAME: DEFAULT_NAME, CONF_FILE_PATH: "mock.file", }, ) await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured" @pytest.mark.usefixtures("mock_setup_entry") async def test_import(hass: HomeAssistant) -> None: """Test import.""" with ( patch("os.path.isfile", Mock(return_value=True)), patch("os.access", Mock(return_value=True)), patch( "homeassistant.components.local_file.camera.mimetypes.guess_type", Mock(return_value=(None, None)), ), ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={ "name": DEFAULT_NAME, "file_path": "mock/path.jpg", }, ) await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY assert result["version"] == 1 assert result["options"] == { CONF_NAME: DEFAULT_NAME, CONF_FILE_PATH: "mock/path.jpg", } @pytest.mark.usefixtures("mock_setup_entry") async def test_import_already_exist( hass: HomeAssistant, loaded_entry: MockConfigEntry ) -> None: """Test import abort existing entry.""" with ( patch("os.path.isfile", Mock(return_value=True)), patch("os.access", Mock(return_value=True)), patch( "homeassistant.components.local_file.camera.mimetypes.guess_type", Mock(return_value=(None, None)), ), ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={ CONF_NAME: DEFAULT_NAME, CONF_FILE_PATH: "mock.file", }, ) await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured"