Avoid circular import in network integration (#58655)
parent
c6157d5520
commit
7516db3600
|
@ -4,22 +4,12 @@ from __future__ import annotations
|
|||
from ipaddress import IPv4Address, IPv6Address, ip_interface
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import websocket_api
|
||||
from homeassistant.components.websocket_api.connection import ActiveConnection
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.loader import bind_hass
|
||||
|
||||
from . import util
|
||||
from .const import (
|
||||
ATTR_ADAPTERS,
|
||||
ATTR_CONFIGURED_ADAPTERS,
|
||||
DOMAIN,
|
||||
IPV4_BROADCAST_ADDR,
|
||||
NETWORK_CONFIG_SCHEMA,
|
||||
)
|
||||
from .const import DOMAIN, IPV4_BROADCAST_ADDR
|
||||
from .models import Adapter
|
||||
from .network import Network
|
||||
|
||||
|
@ -107,50 +97,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||
|
||||
_LOGGER.debug("Adapters: %s", network.adapters)
|
||||
|
||||
websocket_api.async_register_command(hass, websocket_network_adapters)
|
||||
websocket_api.async_register_command(hass, websocket_network_adapters_configure)
|
||||
# Avoid circular issue: http->network->websocket_api->http
|
||||
from .websocket import ( # pylint: disable=import-outside-toplevel
|
||||
async_register_websocket_commands,
|
||||
)
|
||||
|
||||
async_register_websocket_commands(hass)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@websocket_api.require_admin
|
||||
@websocket_api.websocket_command({vol.Required("type"): "network"})
|
||||
@websocket_api.async_response
|
||||
async def websocket_network_adapters(
|
||||
hass: HomeAssistant,
|
||||
connection: ActiveConnection,
|
||||
msg: dict,
|
||||
) -> None:
|
||||
"""Return network preferences."""
|
||||
network: Network = hass.data[DOMAIN]
|
||||
connection.send_result(
|
||||
msg["id"],
|
||||
{
|
||||
ATTR_ADAPTERS: network.adapters,
|
||||
ATTR_CONFIGURED_ADAPTERS: network.configured_adapters,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@websocket_api.require_admin
|
||||
@websocket_api.websocket_command(
|
||||
{
|
||||
vol.Required("type"): "network/configure",
|
||||
vol.Required("config", default={}): NETWORK_CONFIG_SCHEMA,
|
||||
}
|
||||
)
|
||||
@websocket_api.async_response
|
||||
async def websocket_network_adapters_configure(
|
||||
hass: HomeAssistant,
|
||||
connection: ActiveConnection,
|
||||
msg: dict,
|
||||
) -> None:
|
||||
"""Update network config."""
|
||||
network: Network = hass.data[DOMAIN]
|
||||
|
||||
await network.async_reconfig(msg["config"])
|
||||
|
||||
connection.send_result(
|
||||
msg["id"],
|
||||
{ATTR_CONFIGURED_ADAPTERS: network.configured_adapters},
|
||||
)
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
"""The Network Configuration integration websocket commands."""
|
||||
from __future__ import annotations
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import websocket_api
|
||||
from homeassistant.components.websocket_api.connection import ActiveConnection
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
|
||||
from .const import (
|
||||
ATTR_ADAPTERS,
|
||||
ATTR_CONFIGURED_ADAPTERS,
|
||||
DOMAIN,
|
||||
NETWORK_CONFIG_SCHEMA,
|
||||
)
|
||||
from .network import Network
|
||||
|
||||
|
||||
@callback
|
||||
def async_register_websocket_commands(hass: HomeAssistant) -> None:
|
||||
"""Register network websocket commands."""
|
||||
websocket_api.async_register_command(hass, websocket_network_adapters)
|
||||
websocket_api.async_register_command(hass, websocket_network_adapters_configure)
|
||||
|
||||
|
||||
@websocket_api.require_admin
|
||||
@websocket_api.websocket_command({vol.Required("type"): "network"})
|
||||
@websocket_api.async_response
|
||||
async def websocket_network_adapters(
|
||||
hass: HomeAssistant,
|
||||
connection: ActiveConnection,
|
||||
msg: dict,
|
||||
) -> None:
|
||||
"""Return network preferences."""
|
||||
network: Network = hass.data[DOMAIN]
|
||||
connection.send_result(
|
||||
msg["id"],
|
||||
{
|
||||
ATTR_ADAPTERS: network.adapters,
|
||||
ATTR_CONFIGURED_ADAPTERS: network.configured_adapters,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@websocket_api.require_admin
|
||||
@websocket_api.websocket_command(
|
||||
{
|
||||
vol.Required("type"): "network/configure",
|
||||
vol.Required("config", default={}): NETWORK_CONFIG_SCHEMA,
|
||||
}
|
||||
)
|
||||
@websocket_api.async_response
|
||||
async def websocket_network_adapters_configure(
|
||||
hass: HomeAssistant,
|
||||
connection: ActiveConnection,
|
||||
msg: dict,
|
||||
) -> None:
|
||||
"""Update network config."""
|
||||
network: Network = hass.data[DOMAIN]
|
||||
|
||||
await network.async_reconfig(msg["config"])
|
||||
|
||||
connection.send_result(
|
||||
msg["id"],
|
||||
{ATTR_CONFIGURED_ADAPTERS: network.configured_adapters},
|
||||
)
|
Loading…
Reference in New Issue