Use HassKey in ping (#130973)
parent
b9ff8ebe3a
commit
81fc83398b
|
@ -2,7 +2,6 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from icmplib import SocketPermissionError, async_ping
|
||||
|
@ -12,6 +11,7 @@ from homeassistant.const import CONF_HOST, Platform
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.util.hass_dict import HassKey
|
||||
|
||||
from .const import CONF_PING_COUNT, DOMAIN
|
||||
from .coordinator import PingUpdateCoordinator
|
||||
|
@ -21,13 +21,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||
|
||||
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.DEVICE_TRACKER, Platform.SENSOR]
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class PingDomainData:
|
||||
"""Dataclass to store privileged status."""
|
||||
|
||||
privileged: bool | None
|
||||
DATA_PRIVILEGED_KEY: HassKey[bool | None] = HassKey(DOMAIN)
|
||||
|
||||
|
||||
type PingConfigEntry = ConfigEntry[PingUpdateCoordinator]
|
||||
|
@ -35,29 +29,25 @@ type PingConfigEntry = ConfigEntry[PingUpdateCoordinator]
|
|||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up the ping integration."""
|
||||
|
||||
hass.data[DOMAIN] = PingDomainData(
|
||||
privileged=await _can_use_icmp_lib_with_privilege(),
|
||||
)
|
||||
hass.data[DATA_PRIVILEGED_KEY] = await _can_use_icmp_lib_with_privilege()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: PingConfigEntry) -> bool:
|
||||
"""Set up Ping (ICMP) from a config entry."""
|
||||
|
||||
data: PingDomainData = hass.data[DOMAIN]
|
||||
privileged = hass.data[DATA_PRIVILEGED_KEY]
|
||||
|
||||
host: str = entry.options[CONF_HOST]
|
||||
count: int = int(entry.options[CONF_PING_COUNT])
|
||||
ping_cls: type[PingDataICMPLib | PingDataSubProcess]
|
||||
if data.privileged is None:
|
||||
if privileged is None:
|
||||
ping_cls = PingDataSubProcess
|
||||
else:
|
||||
ping_cls = PingDataICMPLib
|
||||
|
||||
coordinator = PingUpdateCoordinator(
|
||||
hass=hass, ping=ping_cls(hass, host, count, data.privileged)
|
||||
hass=hass, ping=ping_cls(hass, host, count, privileged)
|
||||
)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
|
|
Loading…
Reference in New Issue