1 """The ping component."""
3 from __future__
import annotations
7 from icmplib
import SocketPermissionError, async_ping
16 from .const
import CONF_PING_COUNT, DOMAIN
17 from .coordinator
import PingUpdateCoordinator
18 from .helpers
import PingDataICMPLib, PingDataSubProcess
20 _LOGGER = logging.getLogger(__name__)
22 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
23 PLATFORMS = [Platform.BINARY_SENSOR, Platform.DEVICE_TRACKER, Platform.SENSOR]
24 DATA_PRIVILEGED_KEY: HassKey[bool |
None] =
HassKey(DOMAIN)
27 type PingConfigEntry = ConfigEntry[PingUpdateCoordinator]
30 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
31 """Set up the ping integration."""
38 """Set up Ping (ICMP) from a config entry."""
39 privileged = hass.data[DATA_PRIVILEGED_KEY]
41 host: str = entry.options[CONF_HOST]
42 count: int =
int(entry.options[CONF_PING_COUNT])
43 ping_cls: type[PingDataICMPLib | PingDataSubProcess]
44 if privileged
is None:
45 ping_cls = PingDataSubProcess
47 ping_cls = PingDataICMPLib
50 hass=hass, ping=ping_cls(hass, host, count, privileged)
52 await coordinator.async_config_entry_first_refresh()
54 entry.runtime_data = coordinator
56 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
57 entry.async_on_unload(entry.add_update_listener(async_reload_entry))
63 """Handle an options update."""
64 await hass.config_entries.async_reload(entry.entry_id)
68 """Unload a config entry."""
69 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
73 """Verify we can create a raw socket."""
75 await async_ping(
"127.0.0.1", count=0, timeout=0, privileged=
True)
76 except SocketPermissionError:
78 await async_ping(
"127.0.0.1", count=0, timeout=0, privileged=
False)
79 except SocketPermissionError:
81 "Cannot use icmplib because privileges are insufficient to create the"
86 _LOGGER.debug(
"Using icmplib in privileged=False mode")
89 _LOGGER.debug(
"Using icmplib in privileged=True mode")
bool async_unload_entry(HomeAssistant hass, PingConfigEntry entry)
None async_reload_entry(HomeAssistant hass, PingConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_setup_entry(HomeAssistant hass, PingConfigEntry entry)
bool|None _can_use_icmp_lib_with_privilege()