diff --git a/homeassistant/components/hikvision/__init__.py b/homeassistant/components/hikvision/__init__.py index ceede798bcb9..d1f539470e23 100644 --- a/homeassistant/components/hikvision/__init__.py +++ b/homeassistant/components/hikvision/__init__.py @@ -51,7 +51,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: HikvisionConfigEntry) -> try: camera = await hass.async_add_executor_job( - HikCamera, url, port, username, password + HikCamera, url, port, username, password, ssl ) except requests.exceptions.RequestException as err: raise ConfigEntryNotReady(f"Unable to connect to {host}") from err diff --git a/homeassistant/components/hikvision/config_flow.py b/homeassistant/components/hikvision/config_flow.py index c954b9d5edc0..a38cf8d8ed5b 100644 --- a/homeassistant/components/hikvision/config_flow.py +++ b/homeassistant/components/hikvision/config_flow.py @@ -49,7 +49,7 @@ class HikvisionConfigFlow(ConfigFlow, domain=DOMAIN): try: camera = await self.hass.async_add_executor_job( - HikCamera, url, port, username, password + HikCamera, url, port, username, password, ssl ) except requests.exceptions.RequestException: _LOGGER.exception("Error connecting to Hikvision device") @@ -102,7 +102,7 @@ class HikvisionConfigFlow(ConfigFlow, domain=DOMAIN): try: camera = await self.hass.async_add_executor_job( - HikCamera, url, port, username, password + HikCamera, url, port, username, password, ssl ) except requests.exceptions.RequestException: _LOGGER.exception( diff --git a/tests/components/hikvision/test_config_flow.py b/tests/components/hikvision/test_config_flow.py index dd8d5c4233ec..46081077a17f 100644 --- a/tests/components/hikvision/test_config_flow.py +++ b/tests/components/hikvision/test_config_flow.py @@ -63,6 +63,11 @@ async def test_form( CONF_SSL: False, } + # Verify HikCamera was called with the ssl parameter + mock_hikcamera.assert_called_once_with( + f"http://{TEST_HOST}", TEST_PORT, TEST_USERNAME, TEST_PASSWORD, False + ) + async def test_form_cannot_connect( hass: HomeAssistant, @@ -213,6 +218,11 @@ async def test_import_flow( CONF_SSL: False, } + # Verify HikCamera was called with the ssl parameter + mock_hikcamera.assert_called_once_with( + f"http://{TEST_HOST}", TEST_PORT, TEST_USERNAME, TEST_PASSWORD, False + ) + async def test_import_flow_with_defaults( hass: HomeAssistant, @@ -310,3 +320,33 @@ async def test_import_flow_already_configured( assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured" + + +async def test_form_with_ssl( + hass: HomeAssistant, + mock_setup_entry: AsyncMock, + mock_hikcamera: MagicMock, +) -> None: + """Test user flow with ssl enabled passes ssl parameter to HikCamera.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_HOST: TEST_HOST, + CONF_PORT: TEST_PORT, + CONF_USERNAME: TEST_USERNAME, + CONF_PASSWORD: TEST_PASSWORD, + CONF_SSL: True, + }, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["data"][CONF_SSL] is True + + # Verify HikCamera was called with ssl=True + mock_hikcamera.assert_called_once_with( + f"https://{TEST_HOST}", TEST_PORT, TEST_USERNAME, TEST_PASSWORD, True + ) diff --git a/tests/components/hikvision/test_init.py b/tests/components/hikvision/test_init.py index 2314a78143fd..389fbf711830 100644 --- a/tests/components/hikvision/test_init.py +++ b/tests/components/hikvision/test_init.py @@ -5,9 +5,11 @@ from unittest.mock import MagicMock import requests from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import CONF_SSL from homeassistant.core import HomeAssistant from . import setup_integration +from .conftest import TEST_HOST, TEST_PASSWORD, TEST_PORT, TEST_USERNAME from tests.common import MockConfigEntry @@ -23,6 +25,11 @@ async def test_setup_and_unload_entry( assert mock_config_entry.state is ConfigEntryState.LOADED mock_hikcamera.return_value.start_stream.assert_called_once() + # Verify HikCamera was called with the ssl parameter + mock_hikcamera.assert_called_once_with( + f"http://{TEST_HOST}", TEST_PORT, TEST_USERNAME, TEST_PASSWORD, False + ) + await hass.config_entries.async_unload(mock_config_entry.entry_id) await hass.async_block_till_done() @@ -30,6 +37,28 @@ async def test_setup_and_unload_entry( mock_hikcamera.return_value.disconnect.assert_called_once() +async def test_setup_entry_with_ssl( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_hikcamera: MagicMock, +) -> None: + """Test setup with ssl enabled passes ssl parameter to HikCamera.""" + mock_config_entry.add_to_hass(hass) + hass.config_entries.async_update_entry( + mock_config_entry, data={**mock_config_entry.data, CONF_SSL: True} + ) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert mock_config_entry.state is ConfigEntryState.LOADED + + # Verify HikCamera was called with ssl=True + mock_hikcamera.assert_called_once_with( + f"https://{TEST_HOST}", TEST_PORT, TEST_USERNAME, TEST_PASSWORD, True + ) + + async def test_setup_entry_connection_error( hass: HomeAssistant, mock_config_entry: MockConfigEntry,