Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The ping component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from icmplib import SocketPermissionError, async_ping
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_HOST, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import config_validation as cv
13 from homeassistant.helpers.typing import ConfigType
14 from homeassistant.util.hass_dict import HassKey
15 
16 from .const import CONF_PING_COUNT, DOMAIN
17 from .coordinator import PingUpdateCoordinator
18 from .helpers import PingDataICMPLib, PingDataSubProcess
19 
20 _LOGGER = logging.getLogger(__name__)
21 
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)
25 
26 
27 type PingConfigEntry = ConfigEntry[PingUpdateCoordinator]
28 
29 
30 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
31  """Set up the ping integration."""
32  hass.data[DATA_PRIVILEGED_KEY] = await _can_use_icmp_lib_with_privilege()
33 
34  return True
35 
36 
37 async def async_setup_entry(hass: HomeAssistant, entry: PingConfigEntry) -> bool:
38  """Set up Ping (ICMP) from a config entry."""
39  privileged = hass.data[DATA_PRIVILEGED_KEY]
40 
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
46  else:
47  ping_cls = PingDataICMPLib
48 
49  coordinator = PingUpdateCoordinator(
50  hass=hass, ping=ping_cls(hass, host, count, privileged)
51  )
52  await coordinator.async_config_entry_first_refresh()
53 
54  entry.runtime_data = coordinator
55 
56  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
57  entry.async_on_unload(entry.add_update_listener(async_reload_entry))
58 
59  return True
60 
61 
62 async def async_reload_entry(hass: HomeAssistant, entry: PingConfigEntry) -> None:
63  """Handle an options update."""
64  await hass.config_entries.async_reload(entry.entry_id)
65 
66 
67 async def async_unload_entry(hass: HomeAssistant, entry: PingConfigEntry) -> bool:
68  """Unload a config entry."""
69  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
70 
71 
72 async def _can_use_icmp_lib_with_privilege() -> bool | None:
73  """Verify we can create a raw socket."""
74  try:
75  await async_ping("127.0.0.1", count=0, timeout=0, privileged=True)
76  except SocketPermissionError:
77  try:
78  await async_ping("127.0.0.1", count=0, timeout=0, privileged=False)
79  except SocketPermissionError:
80  _LOGGER.debug(
81  "Cannot use icmplib because privileges are insufficient to create the"
82  " socket"
83  )
84  return None
85 
86  _LOGGER.debug("Using icmplib in privileged=False mode")
87  return False
88 
89  _LOGGER.debug("Using icmplib in privileged=True mode")
90  return True
bool async_unload_entry(HomeAssistant hass, PingConfigEntry entry)
Definition: __init__.py:67
None async_reload_entry(HomeAssistant hass, PingConfigEntry entry)
Definition: __init__.py:62
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:30
bool async_setup_entry(HomeAssistant hass, PingConfigEntry entry)
Definition: __init__.py:37
bool|None _can_use_icmp_lib_with_privilege()
Definition: __init__.py:72