Replace util.get_local_ip in favor of components.network.async_get_source_ip() - part 2 (#53368)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Simone Chemelli 2021-09-06 20:44:38 +02:00 committed by GitHub
parent dd7dea9a3f
commit 8b6d0ca13f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 14 deletions

View File

@ -4,7 +4,8 @@ import logging
from aiohttp import web from aiohttp import web
import voluptuous as vol 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 ( from homeassistant.const import (
CONF_ENTITIES, CONF_ENTITIES,
CONF_TYPE, CONF_TYPE,
@ -105,7 +106,9 @@ ATTR_EMULATED_HUE_NAME = "emulated_hue_name"
async def async_setup(hass, yaml_config): async def async_setup(hass, yaml_config):
"""Activate the emulated_hue component.""" """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 = web.Application()
app["hass"] = hass app["hass"] = hass
@ -156,7 +159,6 @@ async def async_setup(hass, yaml_config):
nonlocal protocol nonlocal protocol
nonlocal site nonlocal site
nonlocal runner nonlocal runner
await config.async_setup()
_, protocol = await listen _, protocol = await listen
@ -186,7 +188,7 @@ async def async_setup(hass, yaml_config):
class Config: class Config:
"""Hold configuration variables for the emulated hue bridge.""" """Hold configuration variables for the emulated hue bridge."""
def __init__(self, hass, conf): def __init__(self, hass, conf, local_ip):
"""Initialize the instance.""" """Initialize the instance."""
self.hass = hass self.hass = hass
self.type = conf.get(CONF_TYPE) 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 # Get the IP address that will be passed to the Echo during discovery
self.host_ip_addr = conf.get(CONF_HOST_IP) self.host_ip_addr = conf.get(CONF_HOST_IP)
if self.host_ip_addr is None: if self.host_ip_addr is None:
self.host_ip_addr = util.get_local_ip() self.host_ip_addr = local_ip
_LOGGER.info(
"Listen IP address not specified, auto-detected address is %s",
self.host_ip_addr,
)
# Get the port that the Hue bridge will listen on # Get the port that the Hue bridge will listen on
self.listen_port = conf.get(CONF_LISTEN_PORT) self.listen_port = conf.get(CONF_LISTEN_PORT)

View File

@ -3,6 +3,7 @@
"name": "Emulated Hue", "name": "Emulated Hue",
"documentation": "https://www.home-assistant.io/integrations/emulated_hue", "documentation": "https://www.home-assistant.io/integrations/emulated_hue",
"requirements": ["aiohttp_cors==0.7.0"], "requirements": ["aiohttp_cors==0.7.0"],
"dependencies": ["network"],
"after_dependencies": ["http"], "after_dependencies": ["http"],
"codeowners": [], "codeowners": [],
"quality_scale": "internal", "quality_scale": "internal",

View File

@ -244,6 +244,7 @@ def hue_client(loop, hass_hue, hass_client_no_auth):
"scene.light_off": {emulated_hue.CONF_ENTITY_HIDDEN: False}, "scene.light_off": {emulated_hue.CONF_ENTITY_HIDDEN: False},
}, },
}, },
"127.0.0.1",
) )
config.numbers = ENTITY_IDS_BY_NUMBER 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}, {emulated_hue.DOMAIN: hue_config},
) )
await hass.async_block_till_done() 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 config.numbers = ENTITY_IDS_BY_NUMBER
web_app = hass.http.app web_app = hass.http.app
HueOneLightStateView(config).register(web_app, web_app.router) HueOneLightStateView(config).register(web_app, web_app.router)

View File

@ -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): async def test_config_google_home_entity_id_to_number(hass, hass_storage):
"""Test config adheres to the type.""" """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] = { hass_storage[DATA_KEY] = {
"version": DATA_VERSION, "version": DATA_VERSION,
"key": DATA_KEY, "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): async def test_config_google_home_entity_id_to_number_altered(hass, hass_storage):
"""Test config adheres to the type.""" """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] = { hass_storage[DATA_KEY] = {
"version": DATA_VERSION, "version": DATA_VERSION,
"key": DATA_KEY, "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): async def test_config_google_home_entity_id_to_number_empty(hass, hass_storage):
"""Test config adheres to the type.""" """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": {}} hass_storage[DATA_KEY] = {"version": DATA_VERSION, "key": DATA_KEY, "data": {}}
await conf.async_setup() 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(): def test_config_alexa_entity_id_to_number():
"""Test config adheres to the type.""" """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") number = conf.entity_id_to_number("light.test")
assert number == "light.test" assert number == "light.test"