diff --git a/homeassistant/components/braviatv/__init__.py b/homeassistant/components/braviatv/__init__.py index 52fcf82b38b..1c183b397d8 100644 --- a/homeassistant/components/braviatv/__init__.py +++ b/homeassistant/components/braviatv/__init__.py @@ -11,6 +11,7 @@ from homeassistant.const import CONF_HOST, CONF_MAC, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.aiohttp_client import async_create_clientsession +from .const import CONF_USE_SSL from .coordinator import BraviaTVConfigEntry, BraviaTVCoordinator PLATFORMS: Final[list[Platform]] = [ @@ -26,11 +27,12 @@ async def async_setup_entry( """Set up a config entry.""" host = config_entry.data[CONF_HOST] mac = config_entry.data[CONF_MAC] + ssl = config_entry.data.get(CONF_USE_SSL, False) session = async_create_clientsession( hass, cookie_jar=CookieJar(unsafe=True, quote_cookie=False) ) - client = BraviaClient(host, mac, session=session) + client = BraviaClient(host, mac, session=session, ssl=ssl) coordinator = BraviaTVCoordinator( hass=hass, config_entry=config_entry, diff --git a/homeassistant/components/braviatv/config_flow.py b/homeassistant/components/braviatv/config_flow.py index 1a5aa1fddd6..01ecddad1d4 100644 --- a/homeassistant/components/braviatv/config_flow.py +++ b/homeassistant/components/braviatv/config_flow.py @@ -28,6 +28,7 @@ from .const import ( ATTR_MODEL, CONF_NICKNAME, CONF_USE_PSK, + CONF_USE_SSL, DOMAIN, NICKNAME_PREFIX, ) @@ -46,11 +47,12 @@ class BraviaTVConfigFlow(ConfigFlow, domain=DOMAIN): def create_client(self) -> None: """Create Bravia TV client from config.""" host = self.device_config[CONF_HOST] + ssl = self.device_config[CONF_USE_SSL] session = async_create_clientsession( self.hass, cookie_jar=CookieJar(unsafe=True, quote_cookie=False), ) - self.client = BraviaClient(host=host, session=session) + self.client = BraviaClient(host=host, session=session, ssl=ssl) async def gen_instance_ids(self) -> tuple[str, str]: """Generate client_id and nickname.""" @@ -123,10 +125,10 @@ class BraviaTVConfigFlow(ConfigFlow, domain=DOMAIN): self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Handle authorize step.""" - self.create_client() - if user_input is not None: self.device_config[CONF_USE_PSK] = user_input[CONF_USE_PSK] + self.device_config[CONF_USE_SSL] = user_input[CONF_USE_SSL] + self.create_client() if user_input[CONF_USE_PSK]: return await self.async_step_psk() return await self.async_step_pin() @@ -136,6 +138,7 @@ class BraviaTVConfigFlow(ConfigFlow, domain=DOMAIN): data_schema=vol.Schema( { vol.Required(CONF_USE_PSK, default=False): bool, + vol.Required(CONF_USE_SSL, default=False): bool, } ), ) diff --git a/homeassistant/components/braviatv/const.py b/homeassistant/components/braviatv/const.py index aadd851fc7f..dc9d452dbbc 100644 --- a/homeassistant/components/braviatv/const.py +++ b/homeassistant/components/braviatv/const.py @@ -12,6 +12,7 @@ ATTR_MODEL: Final = "model" CONF_NICKNAME: Final = "nickname" CONF_USE_PSK: Final = "use_psk" +CONF_USE_SSL: Final = "use_ssl" DOMAIN: Final = "braviatv" LEGACY_CLIENT_ID: Final = "HomeAssistant" diff --git a/homeassistant/components/braviatv/strings.json b/homeassistant/components/braviatv/strings.json index c3ba71b547f..9af0e920927 100644 --- a/homeassistant/components/braviatv/strings.json +++ b/homeassistant/components/braviatv/strings.json @@ -15,9 +15,10 @@ "step": { "authorize": { "data": { - "use_psk": "Use PSK authentication" + "use_psk": "Use PSK authentication", + "use_ssl": "Use SSL connection" }, - "description": "Make sure that «Control remotely» is enabled on your TV, go to: \nSettings -> Network -> Remote device settings -> Control remotely. \n\nThere are two authorization methods: PIN code or PSK (Pre-Shared Key). \nAuthorization via PSK is recommended as more stable.", + "description": "Make sure that «Control remotely» is enabled on your TV. Go to: \nSettings -> Network -> Remote device settings -> Control remotely. \n\nThere are two authorization methods: PIN code or PSK (Pre-Shared Key). \nAuthorization via PSK is recommended, as it is more stable. \n\nUse an SSL connection only if your TV supports this connection type.", "title": "Authorize Sony Bravia TV" }, "confirm": { diff --git a/tests/components/braviatv/test_config_flow.py b/tests/components/braviatv/test_config_flow.py index e59d0b6805b..68dd31af6f7 100644 --- a/tests/components/braviatv/test_config_flow.py +++ b/tests/components/braviatv/test_config_flow.py @@ -13,6 +13,7 @@ import pytest from homeassistant.components.braviatv.const import ( CONF_NICKNAME, CONF_USE_PSK, + CONF_USE_SSL, DOMAIN, NICKNAME_PREFIX, ) @@ -131,7 +132,7 @@ async def test_ssdp_discovery(hass: HomeAssistant) -> None: assert result["step_id"] == "authorize" result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={CONF_USE_PSK: False} + result["flow_id"], user_input={CONF_USE_PSK: False, CONF_USE_SSL: False} ) assert result["type"] is FlowResultType.FORM @@ -148,6 +149,7 @@ async def test_ssdp_discovery(hass: HomeAssistant) -> None: CONF_HOST: "bravia-host", CONF_PIN: "1234", CONF_USE_PSK: False, + CONF_USE_SSL: False, CONF_MAC: "AA:BB:CC:DD:EE:FF", CONF_CLIENT_ID: uuid, CONF_NICKNAME: f"{NICKNAME_PREFIX} {uuid[:6]}", @@ -307,8 +309,17 @@ async def test_duplicate_error(hass: HomeAssistant) -> None: assert result["reason"] == "already_configured" -async def test_create_entry(hass: HomeAssistant) -> None: - """Test that entry is added correctly with PIN auth.""" +@pytest.mark.parametrize( + ("use_psk", "use_ssl"), + [ + (True, False), + (False, False), + (True, True), + (False, True), + ], +) +async def test_create_entry(hass: HomeAssistant, use_psk, use_ssl) -> None: + """Test that entry is added correctly.""" uuid = await instance_id.async_get(hass) with ( @@ -328,14 +339,14 @@ async def test_create_entry(hass: HomeAssistant) -> None: assert result["step_id"] == "authorize" result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={CONF_USE_PSK: False} + result["flow_id"], user_input={CONF_USE_PSK: use_psk, CONF_USE_SSL: use_ssl} ) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "pin" + assert result["step_id"] == "psk" if use_psk else "pin" result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={CONF_PIN: "1234"} + result["flow_id"], user_input={CONF_PIN: "secret"} ) assert result["type"] is FlowResultType.CREATE_ENTRY @@ -343,50 +354,18 @@ async def test_create_entry(hass: HomeAssistant) -> None: assert result["title"] == "BRAVIA TV-Model" assert result["data"] == { CONF_HOST: "bravia-host", - CONF_PIN: "1234", - CONF_USE_PSK: False, - CONF_MAC: "AA:BB:CC:DD:EE:FF", - CONF_CLIENT_ID: uuid, - CONF_NICKNAME: f"{NICKNAME_PREFIX} {uuid[:6]}", - } - - -async def test_create_entry_psk(hass: HomeAssistant) -> None: - """Test that entry is added correctly with PSK auth.""" - with ( - patch("pybravia.BraviaClient.connect"), - patch("pybravia.BraviaClient.set_wol_mode"), - patch( - "pybravia.BraviaClient.get_system_info", - return_value=BRAVIA_SYSTEM_INFO, - ), - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data={CONF_HOST: "bravia-host"} - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "authorize" - - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={CONF_USE_PSK: True} - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "psk" - - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={CONF_PIN: "mypsk"} - ) - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["result"].unique_id == "very_unique_string" - assert result["title"] == "BRAVIA TV-Model" - assert result["data"] == { - CONF_HOST: "bravia-host", - CONF_PIN: "mypsk", - CONF_USE_PSK: True, + CONF_PIN: "secret", + CONF_USE_PSK: use_psk, + CONF_USE_SSL: use_ssl, CONF_MAC: "AA:BB:CC:DD:EE:FF", + **( + { + CONF_CLIENT_ID: uuid, + CONF_NICKNAME: f"{NICKNAME_PREFIX} {uuid[:6]}", + } + if not use_psk + else {} + ), }