Use runtime_data in ping (#118332)
This commit is contained in:
parent
76aa504e36
commit
2c99925286
4 changed files with 17 additions and 29 deletions
|
@ -28,7 +28,9 @@ class PingDomainData:
|
||||||
"""Dataclass to store privileged status."""
|
"""Dataclass to store privileged status."""
|
||||||
|
|
||||||
privileged: bool | None
|
privileged: bool | None
|
||||||
coordinators: dict[str, PingUpdateCoordinator]
|
|
||||||
|
|
||||||
|
type PingConfigEntry = ConfigEntry[PingUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
|
@ -36,13 +38,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
|
|
||||||
hass.data[DOMAIN] = PingDomainData(
|
hass.data[DOMAIN] = PingDomainData(
|
||||||
privileged=await _can_use_icmp_lib_with_privilege(),
|
privileged=await _can_use_icmp_lib_with_privilege(),
|
||||||
coordinators={},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> 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."""
|
||||||
|
|
||||||
data: PingDomainData = hass.data[DOMAIN]
|
data: PingDomainData = hass.data[DOMAIN]
|
||||||
|
@ -60,7 +61,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
)
|
)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
data.coordinators[entry.entry_id] = coordinator
|
entry.runtime_data = coordinator
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
|
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
|
||||||
|
@ -68,19 +69,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
async def async_reload_entry(hass: HomeAssistant, entry: PingConfigEntry) -> None:
|
||||||
"""Handle an options update."""
|
"""Handle an options update."""
|
||||||
await hass.config_entries.async_reload(entry.entry_id)
|
await hass.config_entries.async_reload(entry.entry_id)
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: PingConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
if unload_ok:
|
|
||||||
# drop coordinator for config entry
|
|
||||||
hass.data[DOMAIN].coordinators.pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
|
||||||
|
|
||||||
async def _can_use_icmp_lib_with_privilege() -> bool | None:
|
async def _can_use_icmp_lib_with_privilege() -> bool | None:
|
||||||
|
|
|
@ -20,7 +20,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from . import PingDomainData
|
from . import PingConfigEntry
|
||||||
from .const import CONF_IMPORTED_BY, CONF_PING_COUNT, DEFAULT_PING_COUNT, DOMAIN
|
from .const import CONF_IMPORTED_BY, CONF_PING_COUNT, DEFAULT_PING_COUNT, DOMAIN
|
||||||
from .coordinator import PingUpdateCoordinator
|
from .coordinator import PingUpdateCoordinator
|
||||||
from .entity import PingEntity
|
from .entity import PingEntity
|
||||||
|
@ -76,13 +76,10 @@ async def async_setup_platform(
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: PingConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a Ping config entry."""
|
"""Set up a Ping config entry."""
|
||||||
|
async_add_entities([PingBinarySensor(entry, entry.runtime_data)])
|
||||||
data: PingDomainData = hass.data[DOMAIN]
|
|
||||||
|
|
||||||
async_add_entities([PingBinarySensor(entry, data.coordinators[entry.entry_id])])
|
|
||||||
|
|
||||||
|
|
||||||
class PingBinarySensor(PingEntity, BinarySensorEntity):
|
class PingBinarySensor(PingEntity, BinarySensorEntity):
|
||||||
|
|
|
@ -37,7 +37,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from . import PingDomainData
|
from . import PingConfigEntry
|
||||||
from .const import CONF_IMPORTED_BY, CONF_PING_COUNT, DOMAIN
|
from .const import CONF_IMPORTED_BY, CONF_PING_COUNT, DOMAIN
|
||||||
from .coordinator import PingUpdateCoordinator
|
from .coordinator import PingUpdateCoordinator
|
||||||
|
|
||||||
|
@ -125,13 +125,10 @@ async def async_setup_scanner(
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: PingConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a Ping config entry."""
|
"""Set up a Ping config entry."""
|
||||||
|
async_add_entities([PingDeviceTracker(entry, entry.runtime_data)])
|
||||||
data: PingDomainData = hass.data[DOMAIN]
|
|
||||||
|
|
||||||
async_add_entities([PingDeviceTracker(entry, data.coordinators[entry.entry_id])])
|
|
||||||
|
|
||||||
|
|
||||||
class PingDeviceTracker(CoordinatorEntity[PingUpdateCoordinator], ScannerEntity):
|
class PingDeviceTracker(CoordinatorEntity[PingUpdateCoordinator], ScannerEntity):
|
||||||
|
|
|
@ -14,8 +14,7 @@ from homeassistant.const import EntityCategory, UnitOfTime
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import PingDomainData
|
from . import PingConfigEntry
|
||||||
from .const import DOMAIN
|
|
||||||
from .coordinator import PingResult, PingUpdateCoordinator
|
from .coordinator import PingResult, PingUpdateCoordinator
|
||||||
from .entity import PingEntity
|
from .entity import PingEntity
|
||||||
|
|
||||||
|
@ -77,11 +76,10 @@ SENSORS: tuple[PingSensorEntityDescription, ...] = (
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: PingConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Ping sensors from config entry."""
|
"""Set up Ping sensors from config entry."""
|
||||||
data: PingDomainData = hass.data[DOMAIN]
|
coordinator = entry.runtime_data
|
||||||
coordinator = data.coordinators[entry.entry_id]
|
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
PingSensor(entry, description, coordinator)
|
PingSensor(entry, description, coordinator)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue