Move DhcpServiceInfo to service_info helpers (#135658)

* Move DhcpServiceInfo to service_info helpers

* Fix mypy/pylint
pull/135345/head
epenet 2025-01-15 13:09:18 +01:00 committed by GitHub
parent 9c5c1a35a4
commit 31c36beb2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
28 changed files with 94 additions and 39 deletions

View File

@ -10,12 +10,12 @@ from aioairzone.exceptions import AirzoneError, InvalidSystem
from aioairzone.localapi import AirzoneLocalApi, ConnectionOptions
import voluptuous as vol
from homeassistant.components import dhcp
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_ID, CONF_PORT
from homeassistant.data_entry_flow import AbortFlow
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import DOMAIN
@ -93,7 +93,7 @@ class AirZoneConfigFlow(ConfigFlow, domain=DOMAIN):
)
async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
self, discovery_info: DhcpServiceInfo
) -> ConfigFlowResult:
"""Handle DHCP discovery."""
self._discovered_ip = discovery_info.ip

View File

@ -7,7 +7,7 @@ from collections.abc import Callable
from dataclasses import dataclass
from datetime import timedelta
from fnmatch import translate
from functools import lru_cache
from functools import lru_cache, partial
import itertools
import logging
import re
@ -44,12 +44,17 @@ from homeassistant.core import (
State,
callback,
)
from homeassistant.data_entry_flow import BaseServiceInfo
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
discovery_flow,
)
from homeassistant.helpers.deprecation import (
DeprecatedConstant,
all_with_deprecated_constants,
check_if_deprecated_constant,
dir_with_deprecated_constants,
)
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, format_mac
from homeassistant.helpers.discovery_flow import DiscoveryKey
from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -57,6 +62,7 @@ from homeassistant.helpers.event import (
async_track_state_added_domain,
async_track_time_interval,
)
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo as _DhcpServiceInfo
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import DHCPMatcher, async_get_dhcp
@ -74,13 +80,11 @@ SCAN_INTERVAL = timedelta(minutes=60)
_LOGGER = logging.getLogger(__name__)
@dataclass(slots=True)
class DhcpServiceInfo(BaseServiceInfo):
"""Prepared info from dhcp entries."""
ip: str
hostname: str
macaddress: str
_DEPRECATED_DhcpServiceInfo = DeprecatedConstant(
_DhcpServiceInfo,
"homeassistant.helpers.service_info.dhcp.DhcpServiceInfo",
"2026.2",
)
@dataclass(slots=True)
@ -296,7 +300,7 @@ class WatcherBase:
self.hass,
domain,
{"source": config_entries.SOURCE_DHCP},
DhcpServiceInfo(
_DhcpServiceInfo(
ip=ip_address,
hostname=lowercase_hostname,
macaddress=mac_address,
@ -486,3 +490,11 @@ def _memorized_fnmatch(name: str, pattern: str) -> bool:
since the devices will not change frequently
"""
return bool(_compile_fnmatch(pattern).match(name))
# These can be removed if no deprecated constant are in this module anymore
__getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
__dir__ = partial(
dir_with_deprecated_constants, module_globals_keys=[*globals().keys()]
)
__all__ = all_with_deprecated_constants(globals())

View File

@ -9,12 +9,12 @@ from typing import Any, Final
from pyfronius import Fronius, FroniusError
import voluptuous as vol
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import DOMAIN, FroniusConfigEntryData

View File

@ -11,7 +11,6 @@ from fullykiosk import FullyKiosk
from fullykiosk.exceptions import FullyKioskError
import voluptuous as vol
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import (
CONF_HOST,
@ -22,6 +21,7 @@ from homeassistant.const import (
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from homeassistant.helpers.service_info.mqtt import MqttServiceInfo
from .const import DEFAULT_PORT, DOMAIN, LOGGER

View File

@ -11,12 +11,12 @@ from homewizard_energy.models import Device
import voluptuous as vol
from homeassistant.components import onboarding, zeroconf
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_IP_ADDRESS, CONF_PATH
from homeassistant.data_entry_flow import AbortFlow
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.selector import TextSelector
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import (
CONF_API_ENABLED,

View File

@ -13,7 +13,6 @@ from intellifire4py.local_api import IntelliFireAPILocal
from intellifire4py.model import IntelliFireCommonFireplaceData
import voluptuous as vol
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
from homeassistant.const import (
CONF_API_KEY,
@ -22,6 +21,7 @@ from homeassistant.const import (
CONF_PASSWORD,
CONF_USERNAME,
)
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import (
API_MODE_LOCAL,

View File

@ -17,7 +17,6 @@ from homeassistant.components.bluetooth import (
BluetoothServiceInfo,
async_discovered_service_info,
)
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.config_entries import (
SOURCE_REAUTH,
SOURCE_RECONFIGURE,
@ -47,6 +46,7 @@ from homeassistant.helpers.selector import (
TextSelectorConfig,
TextSelectorType,
)
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import CONF_USE_BLUETOOTH, DOMAIN
from .coordinator import LaMarzoccoConfigEntry

View File

@ -23,7 +23,6 @@ from demetriek import (
import voluptuous as vol
from yarl import URL
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.ssdp import (
ATTR_UPNP_FRIENDLY_NAME,
ATTR_UPNP_SERIAL,
@ -44,6 +43,7 @@ from homeassistant.helpers.selector import (
TextSelectorConfig,
TextSelectorType,
)
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from homeassistant.util.network import is_link_local
from .const import DOMAIN, LOGGER

View File

@ -10,11 +10,11 @@ from aiolifx.connection import LIFXConnection
import voluptuous as vol
from homeassistant.components import zeroconf
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_DEVICE, CONF_HOST
from homeassistant.core import callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from homeassistant.helpers.typing import DiscoveryInfoType
from .const import (

View File

@ -17,7 +17,6 @@ from tesla_powerwall import (
)
import voluptuous as vol
from homeassistant.components import dhcp
from homeassistant.config_entries import (
ConfigEntry,
ConfigEntryState,
@ -28,6 +27,7 @@ from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from homeassistant.util.network import is_ip_address
from . import async_last_update_was_successful
@ -116,7 +116,7 @@ class PowerwallConfigFlow(ConfigFlow, domain=DOMAIN):
) and not await _powerwall_is_reachable(ip_address, password)
async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
self, discovery_info: DhcpServiceInfo
) -> ConfigFlowResult:
"""Handle dhcp discovery."""
self.ip_address = discovery_info.ip

View File

@ -9,7 +9,6 @@ from typing import Any
from somfy_mylink_synergy import SomfyMyLinkSynergy
import voluptuous as vol
from homeassistant.components import dhcp
from homeassistant.config_entries import (
ConfigEntry,
ConfigEntryState,
@ -21,6 +20,7 @@ from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import (
CONF_REVERSE,
@ -69,7 +69,7 @@ class SomfyConfigFlow(ConfigFlow, domain=DOMAIN):
self.ip_address: str | None = None
async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
self, discovery_info: DhcpServiceInfo
) -> ConfigFlowResult:
"""Handle dhcp discovery."""
self._async_abort_entries_match({CONF_HOST: discovery_info.ip})

View File

@ -16,7 +16,6 @@ from gotailwind import (
import voluptuous as vol
from homeassistant.components import zeroconf
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_TOKEN
from homeassistant.data_entry_flow import AbortFlow
@ -27,6 +26,7 @@ from homeassistant.helpers.selector import (
TextSelectorConfig,
TextSelectorType,
)
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import DOMAIN, LOGGER

View File

@ -9,11 +9,11 @@ from tesla_wall_connector import WallConnector
from tesla_wall_connector.exceptions import WallConnectorError
import voluptuous as vol
from homeassistant.components import dhcp
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import DOMAIN, WALLCONNECTOR_DEVICE_NAME, WALLCONNECTOR_SERIAL_NUMBER
@ -48,7 +48,7 @@ class TeslaWallConnectorConfigFlow(ConfigFlow, domain=DOMAIN):
self.ip_address: str | None = None
async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
self, discovery_info: DhcpServiceInfo
) -> ConfigFlowResult:
"""Handle dhcp discovery."""
self.ip_address = discovery_info.ip

View File

@ -5,11 +5,11 @@ from typing import Any
from pyvlx import PyVLX, PyVLXException
import voluptuous as vol
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.config_entries import ConfigEntryState, ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME, CONF_PASSWORD
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import DOMAIN, LOGGER

View File

@ -11,11 +11,11 @@ import voluptuous as vol
from wmspro.webcontrol import WebControlPro
from homeassistant.components import dhcp
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import DOMAIN, SUGGESTED_HOST

View File

@ -87,9 +87,9 @@ from .util.enum import try_parse_enum
if TYPE_CHECKING:
from .components.bluetooth import BluetoothServiceInfoBleak
from .components.dhcp import DhcpServiceInfo
from .components.ssdp import SsdpServiceInfo
from .components.usb import UsbServiceInfo
from .helpers.service_info.dhcp import DhcpServiceInfo
from .helpers.service_info.hassio import HassioServiceInfo
from .helpers.service_info.mqtt import MqttServiceInfo
from .helpers.service_info.zeroconf import ZeroconfServiceInfo

View File

@ -16,9 +16,9 @@ if TYPE_CHECKING:
import asyncio
from homeassistant.components.bluetooth import BluetoothServiceInfoBleak
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.ssdp import SsdpServiceInfo
from .service_info.dhcp import DhcpServiceInfo
from .service_info.mqtt import MqttServiceInfo
from .service_info.zeroconf import ZeroconfServiceInfo

View File

@ -0,0 +1,14 @@
"""DHCP discovery data."""
from dataclasses import dataclass
from homeassistant.data_entry_flow import BaseServiceInfo
@dataclass(slots=True)
class DhcpServiceInfo(BaseServiceInfo):
"""Prepared info from dhcp entries."""
ip: str
hostname: str
macaddress: str

View File

@ -34,6 +34,7 @@ from homeassistant.core import HomeAssistant
import homeassistant.helpers.device_registry as dr
from homeassistant.helpers.discovery_flow import DiscoveryKey
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
@ -41,6 +42,7 @@ from tests.common import (
MockConfigEntry,
MockModule,
async_fire_time_changed,
import_and_test_deprecated_constant,
mock_integration,
)
@ -1353,3 +1355,30 @@ async def test_dhcp_rediscover_no_match(
await hass.async_block_till_done()
assert len(mock_init.mock_calls) == 0
@pytest.mark.parametrize(
("constant_name", "replacement_name", "replacement"),
[
(
"DhcpServiceInfo",
"homeassistant.helpers.service_info.dhcp.DhcpServiceInfo",
DhcpServiceInfo,
),
],
)
def test_deprecated_constants(
caplog: pytest.LogCaptureFixture,
constant_name: str,
replacement_name: str,
replacement: Any,
) -> None:
"""Test deprecated automation constants."""
import_and_test_deprecated_constant(
caplog,
dhcp,
constant_name,
replacement_name,
replacement,
"2026.2",
)

View File

@ -6,11 +6,11 @@ from pyfronius import FroniusError
import pytest
from homeassistant import config_entries
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.fronius.const import DOMAIN
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from . import mock_responses

View File

@ -6,7 +6,6 @@ from aiohttp.client_exceptions import ClientConnectorError
from fullykiosk import FullyKioskError
import pytest
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.fully_kiosk.const import DOMAIN
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_MQTT, SOURCE_USER
from homeassistant.const import (
@ -18,6 +17,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from homeassistant.helpers.service_info.mqtt import MqttServiceInfo
from tests.common import MockConfigEntry, load_fixture

View File

@ -10,11 +10,11 @@ from fyta_cli.fyta_exceptions import (
import pytest
from homeassistant import config_entries
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.fyta.const import CONF_EXPIRATION, DOMAIN
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import ACCESS_TOKEN, EXPIRATION, PASSWORD, USERNAME

View File

@ -8,7 +8,6 @@ from pylamarzocco.exceptions import AuthFail, RequestNotSuccessful
from pylamarzocco.models import LaMarzoccoDeviceInfo
import pytest
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.lamarzocco.config_flow import CONF_MACHINE
from homeassistant.components.lamarzocco.const import CONF_USE_BLUETOOTH, DOMAIN
from homeassistant.config_entries import (
@ -28,6 +27,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult, FlowResultType
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from . import USER_INPUT, async_init_integration, get_bluetooth_service_info

View File

@ -13,7 +13,6 @@ from demetriek import (
)
import pytest
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.lametric.const import DOMAIN
from homeassistant.components.ssdp import (
ATTR_UPNP_FRIENDLY_NAME,
@ -25,6 +24,7 @@ from homeassistant.const import CONF_API_KEY, CONF_DEVICE, CONF_HOST, CONF_MAC
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMocker

View File

@ -12,12 +12,12 @@ import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components import zeroconf
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.tailwind.const import DOMAIN
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.const import CONF_HOST, CONF_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from tests.common import MockConfigEntry

View File

@ -7,12 +7,12 @@ from unittest.mock import AsyncMock
import pytest
from pyvlx import PyVLXException
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.velux import DOMAIN
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER, ConfigEntryState
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from tests.common import MockConfigEntry

View File

@ -4,12 +4,12 @@ from unittest.mock import AsyncMock, patch
import pytest
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.withings.const import DOMAIN
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from . import setup_integration
from .conftest import CLIENT_ID, USER_ID

View File

@ -4,12 +4,12 @@ from unittest.mock import AsyncMock, patch
import aiohttp
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.wmspro.const import DOMAIN
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER, ConfigEntryState
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from . import setup_config_entry