From 8b6d0ca13f2499fd67af46431c187ed00d07782e Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Mon, 6 Sep 2021 20:44:38 +0200 Subject: [PATCH] Replace util.get_local_ip in favor of components.network.async_get_source_ip() - part 2 (#53368) Co-authored-by: J. Nick Koston --- .../components/emulated_hue/__init__.py | 16 +++++++--------- .../components/emulated_hue/manifest.json | 1 + tests/components/emulated_hue/test_hue_api.py | 3 ++- tests/components/emulated_hue/test_init.py | 8 ++++---- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/emulated_hue/__init__.py b/homeassistant/components/emulated_hue/__init__.py index 1ee5e19caa7..3cfa710703c 100644 --- a/homeassistant/components/emulated_hue/__init__.py +++ b/homeassistant/components/emulated_hue/__init__.py @@ -4,7 +4,8 @@ import logging from aiohttp import web import voluptuous as vol -from homeassistant import util +from homeassistant.components.network import async_get_source_ip +from homeassistant.components.network.const import PUBLIC_TARGET_IP from homeassistant.const import ( CONF_ENTITIES, CONF_TYPE, @@ -105,7 +106,9 @@ ATTR_EMULATED_HUE_NAME = "emulated_hue_name" async def async_setup(hass, yaml_config): """Activate the emulated_hue component.""" - config = Config(hass, yaml_config.get(DOMAIN, {})) + local_ip = await async_get_source_ip(hass, PUBLIC_TARGET_IP) + config = Config(hass, yaml_config.get(DOMAIN, {}), local_ip) + await config.async_setup() app = web.Application() app["hass"] = hass @@ -156,7 +159,6 @@ async def async_setup(hass, yaml_config): nonlocal protocol nonlocal site nonlocal runner - await config.async_setup() _, protocol = await listen @@ -186,7 +188,7 @@ async def async_setup(hass, yaml_config): class Config: """Hold configuration variables for the emulated hue bridge.""" - def __init__(self, hass, conf): + def __init__(self, hass, conf, local_ip): """Initialize the instance.""" self.hass = hass self.type = conf.get(CONF_TYPE) @@ -204,11 +206,7 @@ class Config: # Get the IP address that will be passed to the Echo during discovery self.host_ip_addr = conf.get(CONF_HOST_IP) if self.host_ip_addr is None: - self.host_ip_addr = util.get_local_ip() - _LOGGER.info( - "Listen IP address not specified, auto-detected address is %s", - self.host_ip_addr, - ) + self.host_ip_addr = local_ip # Get the port that the Hue bridge will listen on self.listen_port = conf.get(CONF_LISTEN_PORT) diff --git a/homeassistant/components/emulated_hue/manifest.json b/homeassistant/components/emulated_hue/manifest.json index 406451639f2..e5a9072e51d 100644 --- a/homeassistant/components/emulated_hue/manifest.json +++ b/homeassistant/components/emulated_hue/manifest.json @@ -3,6 +3,7 @@ "name": "Emulated Hue", "documentation": "https://www.home-assistant.io/integrations/emulated_hue", "requirements": ["aiohttp_cors==0.7.0"], + "dependencies": ["network"], "after_dependencies": ["http"], "codeowners": [], "quality_scale": "internal", diff --git a/tests/components/emulated_hue/test_hue_api.py b/tests/components/emulated_hue/test_hue_api.py index 8515d4e4b0c..e4d422f9802 100644 --- a/tests/components/emulated_hue/test_hue_api.py +++ b/tests/components/emulated_hue/test_hue_api.py @@ -244,6 +244,7 @@ def hue_client(loop, hass_hue, hass_client_no_auth): "scene.light_off": {emulated_hue.CONF_ENTITY_HIDDEN: False}, }, }, + "127.0.0.1", ) config.numbers = ENTITY_IDS_BY_NUMBER @@ -322,7 +323,7 @@ async def test_lights_all_dimmable(hass, hass_client_no_auth): {emulated_hue.DOMAIN: hue_config}, ) await hass.async_block_till_done() - config = Config(None, hue_config) + config = Config(None, hue_config, "127.0.0.1") config.numbers = ENTITY_IDS_BY_NUMBER web_app = hass.http.app HueOneLightStateView(config).register(web_app, web_app.router) diff --git a/tests/components/emulated_hue/test_init.py b/tests/components/emulated_hue/test_init.py index da15fbfba30..2b0d6fe06c6 100644 --- a/tests/components/emulated_hue/test_init.py +++ b/tests/components/emulated_hue/test_init.py @@ -14,7 +14,7 @@ from tests.common import async_fire_time_changed async def test_config_google_home_entity_id_to_number(hass, hass_storage): """Test config adheres to the type.""" - conf = Config(hass, {"type": "google_home"}) + conf = Config(hass, {"type": "google_home"}, "127.0.0.1") hass_storage[DATA_KEY] = { "version": DATA_VERSION, "key": DATA_KEY, @@ -45,7 +45,7 @@ async def test_config_google_home_entity_id_to_number(hass, hass_storage): async def test_config_google_home_entity_id_to_number_altered(hass, hass_storage): """Test config adheres to the type.""" - conf = Config(hass, {"type": "google_home"}) + conf = Config(hass, {"type": "google_home"}, "127.0.0.1") hass_storage[DATA_KEY] = { "version": DATA_VERSION, "key": DATA_KEY, @@ -76,7 +76,7 @@ async def test_config_google_home_entity_id_to_number_altered(hass, hass_storage async def test_config_google_home_entity_id_to_number_empty(hass, hass_storage): """Test config adheres to the type.""" - conf = Config(hass, {"type": "google_home"}) + conf = Config(hass, {"type": "google_home"}, "127.0.0.1") hass_storage[DATA_KEY] = {"version": DATA_VERSION, "key": DATA_KEY, "data": {}} await conf.async_setup() @@ -100,7 +100,7 @@ async def test_config_google_home_entity_id_to_number_empty(hass, hass_storage): def test_config_alexa_entity_id_to_number(): """Test config adheres to the type.""" - conf = Config(None, {"type": "alexa"}) + conf = Config(None, {"type": "alexa"}, "127.0.0.1") number = conf.entity_id_to_number("light.test") assert number == "light.test"