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