From 6ef64f6b1c7dff01313301e3b90592f491fa0bb9 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 11 Nov 2021 22:11:41 -0800 Subject: [PATCH] Fix CORS error in emulated_hue (#59570) --- homeassistant/components/http/view.py | 14 +++++++++----- tests/components/emulated_hue/test_init.py | 11 +++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/http/view.py b/homeassistant/components/http/view.py index aeb610d265e..192d2d5d57b 100644 --- a/homeassistant/components/http/view.py +++ b/homeassistant/components/http/view.py @@ -94,11 +94,15 @@ class HomeAssistantView: for url in urls: routes.append(router.add_route(method, url, handler)) - allow_cors = ( - app["allow_all_cors"] if self.cors_allowed else app["allow_configured_cors"] - ) - for route in routes: - allow_cors(route) + # Use `get` because CORS middleware is not be loaded in emulated_hue + if self.cors_allowed: + allow_cors = app.get("allow_all_cors") + else: + allow_cors = app.get("allow_configured_cors") + + if allow_cors: + for route in routes: + allow_cors(route) def request_handler_factory( diff --git a/tests/components/emulated_hue/test_init.py b/tests/components/emulated_hue/test_init.py index 2b0d6fe06c6..93bf8c0631f 100644 --- a/tests/components/emulated_hue/test_init.py +++ b/tests/components/emulated_hue/test_init.py @@ -1,5 +1,6 @@ """Test the Emulated Hue component.""" from datetime import timedelta +from unittest.mock import patch from homeassistant.components.emulated_hue import ( DATA_KEY, @@ -7,6 +8,7 @@ from homeassistant.components.emulated_hue import ( SAVE_DELAY, Config, ) +from homeassistant.setup import async_setup_component from homeassistant.util import utcnow from tests.common import async_fire_time_changed @@ -113,3 +115,12 @@ def test_config_alexa_entity_id_to_number(): entity_id = conf.number_to_entity_id("light.test") assert entity_id == "light.test" + + +async def test_setup_works(hass): + """Test setup works.""" + hass.config.components.add("network") + with patch( + "homeassistant.components.emulated_hue.create_upnp_datagram_endpoint" + ), patch("homeassistant.components.emulated_hue.async_get_source_ip"): + assert await async_setup_component(hass, "emulated_hue", {})