diff --git a/homeassistant/components/roku/.translations/en.json b/homeassistant/components/roku/.translations/en.json index 8dccd065121..45a2dd8dcba 100644 --- a/homeassistant/components/roku/.translations/en.json +++ b/homeassistant/components/roku/.translations/en.json @@ -1,11 +1,11 @@ { "config": { "abort": { - "already_configured": "Roku device is already configured" + "already_configured": "Roku device is already configured", + "unknown": "Unexpected error" }, "error": { - "cannot_connect": "Failed to connect, please try again", - "unknown": "Unexpected error" + "cannot_connect": "Failed to connect, please try again" }, "flow_title": "Roku: {name}", "step": { diff --git a/homeassistant/components/roku/config_flow.py b/homeassistant/components/roku/config_flow.py index 32e66901e0f..eec5683c95d 100644 --- a/homeassistant/components/roku/config_flow.py +++ b/homeassistant/components/roku/config_flow.py @@ -34,8 +34,13 @@ def validate_input(data: Dict) -> Dict: Data has the keys from DATA_SCHEMA with values provided by the user. """ - roku = Roku(data["host"]) - device_info = roku.device_info + try: + roku = Roku(data["host"]) + device_info = roku.device_info + except (SocketGIAError, RequestException, RokuException) as exception: + raise CannotConnect from exception + except Exception as exception: # pylint: disable=broad-except + raise UnknownError from exception return { "title": data["host"], @@ -74,11 +79,11 @@ class RokuConfigFlow(ConfigFlow, domain=DOMAIN): try: info = await self.hass.async_add_executor_job(validate_input, user_input) - except (SocketGIAError, RequestException, RokuException): + except CannotConnect: errors["base"] = ERROR_CANNOT_CONNECT return self._show_form(errors) - except Exception: # pylint: disable=broad-except - _LOGGER.exception("Unknown error trying to connect.") + except UnknownError: + _LOGGER.exception("Unknown error trying to connect") return self.async_abort(reason=ERROR_UNKNOWN) await self.async_set_unique_id(info["serial_num"]) @@ -119,10 +124,10 @@ class RokuConfigFlow(ConfigFlow, domain=DOMAIN): try: await self.hass.async_add_executor_job(validate_input, user_input) return self.async_create_entry(title=name, data=user_input) - except (SocketGIAError, RequestException, RokuException): + except CannotConnect: return self.async_abort(reason=ERROR_CANNOT_CONNECT) - except Exception: # pylint: disable=broad-except - _LOGGER.exception("Unknown error trying to connect.") + except UnknownError: + _LOGGER.exception("Unknown error trying to connect") return self.async_abort(reason=ERROR_UNKNOWN) return self.async_show_form( @@ -132,3 +137,7 @@ class RokuConfigFlow(ConfigFlow, domain=DOMAIN): class CannotConnect(HomeAssistantError): """Error to indicate we cannot connect.""" + + +class UnknownError(HomeAssistantError): + """Error to indicate we encountered an unknown error.""" diff --git a/homeassistant/components/roku/strings.json b/homeassistant/components/roku/strings.json index 0069728d14a..2beba3433b5 100644 --- a/homeassistant/components/roku/strings.json +++ b/homeassistant/components/roku/strings.json @@ -17,11 +17,11 @@ } }, "error": { - "cannot_connect": "Failed to connect, please try again", - "unknown": "Unexpected error" + "cannot_connect": "Failed to connect, please try again" }, "abort": { - "already_configured": "Roku device is already configured" + "already_configured": "Roku device is already configured", + "unknown": "Unexpected error" } } } diff --git a/tests/components/roku/test_config_flow.py b/tests/components/roku/test_config_flow.py index 93d3fbb938d..9aa60d8594c 100644 --- a/tests/components/roku/test_config_flow.py +++ b/tests/components/roku/test_config_flow.py @@ -124,10 +124,12 @@ async def test_form_cannot_connect(hass: HomeAssistantType) -> None: ) with patch( - "homeassistant.components.roku.config_flow.validate_input", + "homeassistant.components.roku.config_flow.Roku._call", side_effect=RokuException, ) as mock_validate_input: - result = await async_configure_flow(hass, result["flow_id"], {CONF_HOST: HOST},) + result = await hass.config_entries.flow.async_configure( + flow_id=result["flow_id"], user_input={CONF_HOST: HOST} + ) assert result["type"] == RESULT_TYPE_FORM assert result["errors"] == {"base": "cannot_connect"} @@ -143,10 +145,12 @@ async def test_form_cannot_connect_request(hass: HomeAssistantType) -> None: ) with patch( - "homeassistant.components.roku.config_flow.validate_input", + "homeassistant.components.roku.config_flow.Roku._call", side_effect=RequestException, ) as mock_validate_input: - result = await async_configure_flow(hass, result["flow_id"], {CONF_HOST: HOST},) + result = await hass.config_entries.flow.async_configure( + flow_id=result["flow_id"], user_input={CONF_HOST: HOST} + ) assert result["type"] == RESULT_TYPE_FORM assert result["errors"] == {"base": "cannot_connect"} @@ -162,10 +166,12 @@ async def test_form_cannot_connect_socket(hass: HomeAssistantType) -> None: ) with patch( - "homeassistant.components.roku.config_flow.validate_input", + "homeassistant.components.roku.config_flow.Roku._call", side_effect=SocketGIAError, ) as mock_validate_input: - result = await async_configure_flow(hass, result["flow_id"], {CONF_HOST: HOST},) + result = await hass.config_entries.flow.async_configure( + flow_id=result["flow_id"], user_input={CONF_HOST: HOST} + ) assert result["type"] == RESULT_TYPE_FORM assert result["errors"] == {"base": "cannot_connect"} @@ -181,10 +187,11 @@ async def test_form_unknown_error(hass: HomeAssistantType) -> None: ) with patch( - "homeassistant.components.roku.config_flow.validate_input", - side_effect=Exception, + "homeassistant.components.roku.config_flow.Roku._call", side_effect=Exception, ) as mock_validate_input: - result = await async_configure_flow(hass, result["flow_id"], {CONF_HOST: HOST},) + result = await hass.config_entries.flow.async_configure( + flow_id=result["flow_id"], user_input={CONF_HOST: HOST} + ) assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "unknown"