Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dataclass to carry data in ping #99803

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions homeassistant/components/ping/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,19 +11,28 @@
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


Expand Down
8 changes: 6 additions & 2 deletions homeassistant/components/ping/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
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__)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions homeassistant/components/ping/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@

DOMAIN = "ping"
PLATFORMS = [Platform.BINARY_SENSOR]

PING_PRIVS = "ping_privs"
7 changes: 5 additions & 2 deletions homeassistant/components/ping/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
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__)

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/ping/manifest.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down