mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-23 09:06:29 +00:00
Return default network connection (#2115)
This commit is contained in:
parent
4c525de5e2
commit
ac4277cd7b
@ -39,6 +39,7 @@ SCHEMA_UPDATE = vol.Schema(
|
||||
def interface_information(interface: NetworkInterface) -> dict:
|
||||
"""Return a dict with information of a interface to be used in th API."""
|
||||
return {
|
||||
ATTR_INTERFACE: interface.name,
|
||||
ATTR_IP_ADDRESS: f"{interface.ip_address}/{interface.prefix}",
|
||||
ATTR_GATEWAY: interface.gateway,
|
||||
ATTR_ID: interface.id,
|
||||
@ -69,8 +70,19 @@ class APINetwork(CoreSysAttributes):
|
||||
async def interface_info(self, request: web.Request) -> Dict[str, Any]:
|
||||
"""Return network information for a interface."""
|
||||
req_interface = request.match_info.get(ATTR_INTERFACE)
|
||||
|
||||
if req_interface.lower() == "default":
|
||||
for interface in self.sys_host.network.interfaces:
|
||||
if req_interface == self.sys_host.network.interfaces[interface].name:
|
||||
if not self.sys_host.network.interfaces[interface].primary:
|
||||
continue
|
||||
return interface_information(
|
||||
self.sys_host.network.interfaces[interface]
|
||||
)
|
||||
|
||||
else:
|
||||
for interface in self.sys_host.network.interfaces:
|
||||
if req_interface != self.sys_host.network.interfaces[interface].name:
|
||||
continue
|
||||
return interface_information(
|
||||
self.sys_host.network.interfaces[interface]
|
||||
)
|
||||
|
@ -18,6 +18,16 @@ async def test_api_network_interface_info(api_client):
|
||||
resp = await api_client.get(f"/network/interface/{TEST_INTERFACE}/info")
|
||||
result = await resp.json()
|
||||
assert result["data"]["ip_address"] == "192.168.2.148/24"
|
||||
assert result["data"]["interface"] == TEST_INTERFACE
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_network_interface_info_default(api_client):
|
||||
"""Test network manager default api."""
|
||||
resp = await api_client.get("/network/interface/default/info")
|
||||
result = await resp.json()
|
||||
assert result["data"]["ip_address"] == "192.168.2.148/24"
|
||||
assert result["data"]["interface"] == TEST_INTERFACE
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
Loading…
x
Reference in New Issue
Block a user