mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Replace util.get_local_ip in favor of components.network.async_get_source_ip() - part 1 (#52980)
This commit is contained in:
parent
3461f61f9f
commit
75f7d3d696
@ -3,6 +3,7 @@
|
|||||||
"name": "DLNA Digital Media Renderer",
|
"name": "DLNA Digital Media Renderer",
|
||||||
"documentation": "https://www.home-assistant.io/integrations/dlna_dmr",
|
"documentation": "https://www.home-assistant.io/integrations/dlna_dmr",
|
||||||
"requirements": ["async-upnp-client==0.19.1"],
|
"requirements": ["async-upnp-client==0.19.1"],
|
||||||
|
"dependencies": ["network"],
|
||||||
"codeowners": [],
|
"codeowners": [],
|
||||||
"iot_class": "local_push"
|
"iot_class": "local_push"
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,8 @@ from homeassistant.components.media_player.const import (
|
|||||||
SUPPORT_VOLUME_MUTE,
|
SUPPORT_VOLUME_MUTE,
|
||||||
SUPPORT_VOLUME_SET,
|
SUPPORT_VOLUME_SET,
|
||||||
)
|
)
|
||||||
|
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_NAME,
|
CONF_NAME,
|
||||||
CONF_URL,
|
CONF_URL,
|
||||||
@ -38,7 +40,6 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.util import get_local_ip
|
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -142,7 +143,7 @@ async def async_setup_platform(
|
|||||||
async with hass.data[DLNA_DMR_DATA]["lock"]:
|
async with hass.data[DLNA_DMR_DATA]["lock"]:
|
||||||
server_host = config.get(CONF_LISTEN_IP)
|
server_host = config.get(CONF_LISTEN_IP)
|
||||||
if server_host is None:
|
if server_host is None:
|
||||||
server_host = get_local_ip()
|
server_host = await async_get_source_ip(hass, PUBLIC_TARGET_IP)
|
||||||
server_port = config.get(CONF_LISTEN_PORT, DEFAULT_LISTEN_PORT)
|
server_port = config.get(CONF_LISTEN_PORT, DEFAULT_LISTEN_PORT)
|
||||||
callback_url_override = config.get(CONF_CALLBACK_URL_OVERRIDE)
|
callback_url_override = config.get(CONF_CALLBACK_URL_OVERRIDE)
|
||||||
event_handler = await async_start_event_handler(
|
event_handler = await async_start_event_handler(
|
||||||
|
@ -16,13 +16,14 @@ from fritzconnection.core.exceptions import (
|
|||||||
import slugify as unicode_slug
|
import slugify as unicode_slug
|
||||||
import xmltodict
|
import xmltodict
|
||||||
|
|
||||||
|
from homeassistant.components.network import async_get_source_ip
|
||||||
from homeassistant.components.switch import SwitchEntity
|
from homeassistant.components.switch import SwitchEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util import get_local_ip, slugify
|
from homeassistant.util import slugify
|
||||||
|
|
||||||
from .common import (
|
from .common import (
|
||||||
FritzBoxBaseEntity,
|
FritzBoxBaseEntity,
|
||||||
@ -161,7 +162,7 @@ def deflection_entities_list(
|
|||||||
|
|
||||||
|
|
||||||
def port_entities_list(
|
def port_entities_list(
|
||||||
fritzbox_tools: FritzBoxTools, device_friendly_name: str
|
fritzbox_tools: FritzBoxTools, device_friendly_name: str, local_ip: str
|
||||||
) -> list[FritzBoxPortSwitch]:
|
) -> list[FritzBoxPortSwitch]:
|
||||||
"""Get list of port forwarding entities."""
|
"""Get list of port forwarding entities."""
|
||||||
|
|
||||||
@ -194,7 +195,6 @@ def port_entities_list(
|
|||||||
port_forwards_count,
|
port_forwards_count,
|
||||||
)
|
)
|
||||||
|
|
||||||
local_ip = get_local_ip()
|
|
||||||
_LOGGER.debug("IP source for %s is %s", fritzbox_tools.host, local_ip)
|
_LOGGER.debug("IP source for %s is %s", fritzbox_tools.host, local_ip)
|
||||||
|
|
||||||
for i in range(port_forwards_count):
|
for i in range(port_forwards_count):
|
||||||
@ -290,12 +290,15 @@ def profile_entities_list(
|
|||||||
|
|
||||||
|
|
||||||
def all_entities_list(
|
def all_entities_list(
|
||||||
fritzbox_tools: FritzBoxTools, device_friendly_name: str, data_fritz: FritzData
|
fritzbox_tools: FritzBoxTools,
|
||||||
|
device_friendly_name: str,
|
||||||
|
data_fritz: FritzData,
|
||||||
|
local_ip: str,
|
||||||
) -> list[Entity]:
|
) -> list[Entity]:
|
||||||
"""Get a list of all entities."""
|
"""Get a list of all entities."""
|
||||||
return [
|
return [
|
||||||
*deflection_entities_list(fritzbox_tools, device_friendly_name),
|
*deflection_entities_list(fritzbox_tools, device_friendly_name),
|
||||||
*port_entities_list(fritzbox_tools, device_friendly_name),
|
*port_entities_list(fritzbox_tools, device_friendly_name, local_ip),
|
||||||
*wifi_entities_list(fritzbox_tools, device_friendly_name),
|
*wifi_entities_list(fritzbox_tools, device_friendly_name),
|
||||||
*profile_entities_list(fritzbox_tools, data_fritz),
|
*profile_entities_list(fritzbox_tools, data_fritz),
|
||||||
]
|
]
|
||||||
@ -311,8 +314,12 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
_LOGGER.debug("Fritzbox services: %s", fritzbox_tools.connection.services)
|
_LOGGER.debug("Fritzbox services: %s", fritzbox_tools.connection.services)
|
||||||
|
|
||||||
|
local_ip = await async_get_source_ip(
|
||||||
|
fritzbox_tools.hass, target_ip=fritzbox_tools.host
|
||||||
|
)
|
||||||
|
|
||||||
entities_list = await hass.async_add_executor_job(
|
entities_list = await hass.async_add_executor_job(
|
||||||
all_entities_list, fritzbox_tools, entry.title, data_fritz
|
all_entities_list, fritzbox_tools, entry.title, data_fritz, local_ip
|
||||||
)
|
)
|
||||||
|
|
||||||
async_add_entities(entities_list)
|
async_add_entities(entities_list)
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
"name": "Local IP Address",
|
"name": "Local IP Address",
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"documentation": "https://www.home-assistant.io/integrations/local_ip",
|
"documentation": "https://www.home-assistant.io/integrations/local_ip",
|
||||||
|
"dependencies": ["network"],
|
||||||
"codeowners": ["@issacg"],
|
"codeowners": ["@issacg"],
|
||||||
"iot_class": "local_polling"
|
"iot_class": "local_polling"
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
"""Sensor platform for local_ip."""
|
"""Sensor platform for local_ip."""
|
||||||
|
|
||||||
|
from homeassistant.components.network import async_get_source_ip
|
||||||
|
from homeassistant.components.network.const import PUBLIC_TARGET_IP
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util import get_local_ip
|
|
||||||
|
|
||||||
from .const import DOMAIN, SENSOR
|
from .const import DOMAIN, SENSOR
|
||||||
|
|
||||||
@ -30,6 +31,8 @@ class IPSensor(SensorEntity):
|
|||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
|
|
||||||
def update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Fetch new state data for the sensor."""
|
"""Fetch new state data for the sensor."""
|
||||||
self._attr_state = get_local_ip()
|
self._attr_state = await async_get_source_ip(
|
||||||
|
self.hass, target_ip=PUBLIC_TARGET_IP
|
||||||
|
)
|
||||||
|
@ -33,7 +33,7 @@ async def async_get_adapters(hass: HomeAssistant) -> list[Adapter]:
|
|||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
async def async_get_source_ip(hass: HomeAssistant, target_ip: str) -> str | None:
|
async def async_get_source_ip(hass: HomeAssistant, target_ip: str) -> str:
|
||||||
"""Get the source ip for a target ip."""
|
"""Get the source ip for a target ip."""
|
||||||
adapters = await async_get_adapters(hass)
|
adapters = await async_get_adapters(hass)
|
||||||
all_ipv4s = []
|
all_ipv4s = []
|
||||||
|
@ -16,7 +16,7 @@ ATTR_CONFIGURED_ADAPTERS: Final = "configured_adapters"
|
|||||||
DEFAULT_CONFIGURED_ADAPTERS: list[str] = []
|
DEFAULT_CONFIGURED_ADAPTERS: list[str] = []
|
||||||
|
|
||||||
MDNS_TARGET_IP: Final = "224.0.0.251"
|
MDNS_TARGET_IP: Final = "224.0.0.251"
|
||||||
|
PUBLIC_TARGET_IP: Final = "8.8.8.8"
|
||||||
|
|
||||||
NETWORK_CONFIG_SCHEMA = vol.Schema(
|
NETWORK_CONFIG_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
|
@ -6,12 +6,13 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components import ssdp
|
from homeassistant.components import ssdp
|
||||||
|
from homeassistant.components.network import async_get_source_ip
|
||||||
|
from homeassistant.components.network.const import PUBLIC_TARGET_IP
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.util import get_local_ip
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_LOCAL_IP,
|
CONF_LOCAL_IP,
|
||||||
@ -63,7 +64,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType):
|
|||||||
_LOGGER.debug("async_setup, config: %s", config)
|
_LOGGER.debug("async_setup, config: %s", config)
|
||||||
conf_default = CONFIG_SCHEMA({DOMAIN: {}})[DOMAIN]
|
conf_default = CONFIG_SCHEMA({DOMAIN: {}})[DOMAIN]
|
||||||
conf = config.get(DOMAIN, conf_default)
|
conf = config.get(DOMAIN, conf_default)
|
||||||
local_ip = await hass.async_add_executor_job(get_local_ip)
|
local_ip = await async_get_source_ip(hass, PUBLIC_TARGET_IP)
|
||||||
hass.data[DOMAIN] = {
|
hass.data[DOMAIN] = {
|
||||||
DOMAIN_CONFIG: conf,
|
DOMAIN_CONFIG: conf,
|
||||||
DOMAIN_DEVICES: {},
|
DOMAIN_DEVICES: {},
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"documentation": "https://www.home-assistant.io/integrations/upnp",
|
"documentation": "https://www.home-assistant.io/integrations/upnp",
|
||||||
"requirements": ["async-upnp-client==0.19.1"],
|
"requirements": ["async-upnp-client==0.19.1"],
|
||||||
"dependencies": ["ssdp"],
|
"dependencies": ["network", "ssdp"],
|
||||||
"codeowners": ["@StevenLooman"],
|
"codeowners": ["@StevenLooman"],
|
||||||
"ssdp": [
|
"ssdp": [
|
||||||
{
|
{
|
||||||
|
@ -19,8 +19,9 @@ from zeroconf import (
|
|||||||
)
|
)
|
||||||
from zeroconf.asyncio import AsyncServiceInfo
|
from zeroconf.asyncio import AsyncServiceInfo
|
||||||
|
|
||||||
from homeassistant import config_entries, util
|
from homeassistant import config_entries
|
||||||
from homeassistant.components import network
|
from homeassistant.components import network
|
||||||
|
from homeassistant.components.network import async_get_source_ip
|
||||||
from homeassistant.components.network.models import Adapter
|
from homeassistant.components.network.models import Adapter
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
EVENT_HOMEASSISTANT_START,
|
EVENT_HOMEASSISTANT_START,
|
||||||
@ -222,7 +223,7 @@ async def _async_register_hass_zc_service(
|
|||||||
# Set old base URL based on external or internal
|
# Set old base URL based on external or internal
|
||||||
params["base_url"] = params["external_url"] or params["internal_url"]
|
params["base_url"] = params["external_url"] or params["internal_url"]
|
||||||
|
|
||||||
host_ip = util.get_local_ip()
|
host_ip = await async_get_source_ip(hass, target_ip=MDNS_TARGET_IP)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
host_ip_pton = socket.inet_pton(socket.AF_INET, host_ip)
|
host_ip_pton = socket.inet_pton(socket.AF_INET, host_ip)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Tests for the local_ip component."""
|
"""Tests for the local_ip component."""
|
||||||
from homeassistant.components.local_ip import DOMAIN
|
from homeassistant.components.local_ip import DOMAIN
|
||||||
from homeassistant.util import get_local_ip
|
from homeassistant.components.network import async_get_source_ip
|
||||||
|
from homeassistant.components.zeroconf import MDNS_TARGET_IP
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
@ -13,7 +14,7 @@ async def test_basic_setup(hass):
|
|||||||
await hass.config_entries.async_setup(entry.entry_id)
|
await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
local_ip = await hass.async_add_executor_job(get_local_ip)
|
local_ip = await async_get_source_ip(hass, target_ip=MDNS_TARGET_IP)
|
||||||
state = hass.states.get(f"sensor.{DOMAIN}")
|
state = hass.states.get(f"sensor.{DOMAIN}")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == local_ip
|
assert state.state == local_ip
|
||||||
|
Loading…
x
Reference in New Issue
Block a user