mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Use dataclass to carry data in ping (#99803)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
b881057aa6
commit
3c455391c2
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
@ -16,5 +16,3 @@ PING_ATTEMPTS_COUNT = 3
|
||||
|
||||
DOMAIN = "ping"
|
||||
PLATFORMS = [Platform.BINARY_SENSOR]
|
||||
|
||||
PING_PRIVS = "ping_privs"
|
||||
|
@ -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,
|
||||
|
@ -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"],
|
||||
|
Loading…
x
Reference in New Issue
Block a user