diff --git a/CODEOWNERS b/CODEOWNERS index 95c5b7bedcf..6d2637c1f3e 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -949,6 +949,8 @@ build.json @home-assistant/supervisor /tests/components/picnic/ @corneyl /homeassistant/components/pilight/ @trekky12 /tests/components/pilight/ @trekky12 +/homeassistant/components/ping/ @jpbede +/tests/components/ping/ @jpbede /homeassistant/components/plaato/ @JohNan /tests/components/plaato/ @JohNan /homeassistant/components/plex/ @jjlawren diff --git a/homeassistant/components/ping/__init__.py b/homeassistant/components/ping/__init__.py index 3ff36f2e283..26dd8113231 100644 --- a/homeassistant/components/ping/__init__.py +++ b/homeassistant/components/ping/__init__.py @@ -1,6 +1,7 @@ """The ping component.""" from __future__ import annotations +from dataclasses import dataclass import logging from icmplib import SocketPermissionError, ping as icmp_ping @@ -10,19 +11,28 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.typing import ConfigType -from .const import DOMAIN, PING_PRIVS, PLATFORMS +from .const import DOMAIN, PLATFORMS _LOGGER = logging.getLogger(__name__) CONFIG_SCHEMA = cv.platform_only_config_schema(DOMAIN) +@dataclass(slots=True) +class PingDomainData: + """Dataclass to store privileged status.""" + + privileged: bool | None + + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the ping integration.""" await async_setup_reload_service(hass, DOMAIN, PLATFORMS) - hass.data[DOMAIN] = { - PING_PRIVS: await hass.async_add_executor_job(_can_use_icmp_lib_with_privilege), - } + + hass.data[DOMAIN] = PingDomainData( + privileged=await hass.async_add_executor_job(_can_use_icmp_lib_with_privilege), + ) + return True diff --git a/homeassistant/components/ping/binary_sensor.py b/homeassistant/components/ping/binary_sensor.py index 6a150b3dc4c..bab7f3a3735 100644 --- a/homeassistant/components/ping/binary_sensor.py +++ b/homeassistant/components/ping/binary_sensor.py @@ -23,7 +23,8 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from .const import DOMAIN, ICMP_TIMEOUT, PING_PRIVS, PING_TIMEOUT +from . import PingDomainData +from .const import DOMAIN, ICMP_TIMEOUT, PING_TIMEOUT _LOGGER = logging.getLogger(__name__) @@ -70,10 +71,13 @@ async def async_setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the Ping Binary sensor.""" + + data: PingDomainData = hass.data[DOMAIN] + host: str = config[CONF_HOST] count: int = config[CONF_PING_COUNT] name: str = config.get(CONF_NAME, f"{DEFAULT_NAME} {host}") - privileged: bool | None = hass.data[DOMAIN][PING_PRIVS] + privileged: bool | None = data.privileged ping_cls: type[PingDataSubProcess | PingDataICMPLib] if privileged is None: ping_cls = PingDataSubProcess diff --git a/homeassistant/components/ping/const.py b/homeassistant/components/ping/const.py index 1a77c62fa5c..fd70a9340c2 100644 --- a/homeassistant/components/ping/const.py +++ b/homeassistant/components/ping/const.py @@ -16,5 +16,3 @@ PING_ATTEMPTS_COUNT = 3 DOMAIN = "ping" PLATFORMS = [Platform.BINARY_SENSOR] - -PING_PRIVS = "ping_privs" diff --git a/homeassistant/components/ping/device_tracker.py b/homeassistant/components/ping/device_tracker.py index a25b3652b36..9a63a2f844d 100644 --- a/homeassistant/components/ping/device_tracker.py +++ b/homeassistant/components/ping/device_tracker.py @@ -25,7 +25,8 @@ from homeassistant.util import dt as dt_util from homeassistant.util.async_ import gather_with_limited_concurrency from homeassistant.util.process import kill_subprocess -from .const import DOMAIN, ICMP_TIMEOUT, PING_ATTEMPTS_COUNT, PING_PRIVS, PING_TIMEOUT +from . import PingDomainData +from .const import DOMAIN, ICMP_TIMEOUT, PING_ATTEMPTS_COUNT, PING_TIMEOUT _LOGGER = logging.getLogger(__name__) @@ -97,7 +98,9 @@ async def async_setup_scanner( ) -> bool: """Set up the Host objects and return the update function.""" - privileged = hass.data[DOMAIN][PING_PRIVS] + data: PingDomainData = hass.data[DOMAIN] + + privileged = data.privileged ip_to_dev_id = {ip: dev_id for (dev_id, ip) in config[CONF_HOSTS].items()} interval = config.get( CONF_SCAN_INTERVAL, diff --git a/homeassistant/components/ping/manifest.json b/homeassistant/components/ping/manifest.json index 9290c9992eb..e27c3a239d0 100644 --- a/homeassistant/components/ping/manifest.json +++ b/homeassistant/components/ping/manifest.json @@ -1,7 +1,7 @@ { "domain": "ping", "name": "Ping (ICMP)", - "codeowners": [], + "codeowners": ["@jpbede"], "documentation": "https://www.home-assistant.io/integrations/ping", "iot_class": "local_polling", "loggers": ["icmplib"],