2019-04-03 17:40:03 +02:00
|
|
|
"""Tracks the latency of a host by sending ICMP echo requests (ping)."""
|
2024-03-08 15:04:07 +01:00
|
|
|
|
2021-03-18 13:21:46 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2020-09-12 20:21:57 +02:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-16 09:38:02 -05:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-12 20:21:57 +02:00
|
|
|
BinarySensorEntity,
|
|
|
|
|
)
|
2024-06-22 20:39:32 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
2025-02-10 21:08:03 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
2020-08-05 00:43:35 -10:00
|
|
|
|
2024-06-22 20:39:32 +02:00
|
|
|
from .const import CONF_IMPORTED_BY
|
2025-02-09 14:44:37 +01:00
|
|
|
from .coordinator import PingConfigEntry, PingUpdateCoordinator
|
2024-03-03 11:08:28 +01:00
|
|
|
from .entity import PingEntity
|
2017-04-20 08:15:26 +02:00
|
|
|
|
2023-11-17 20:30:30 +01:00
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
2025-02-10 21:08:03 +01:00
|
|
|
hass: HomeAssistant,
|
|
|
|
|
entry: PingConfigEntry,
|
|
|
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
2023-11-17 20:30:30 +01:00
|
|
|
) -> None:
|
|
|
|
|
"""Set up a Ping config entry."""
|
2024-05-29 08:12:54 +02:00
|
|
|
async_add_entities([PingBinarySensor(entry, entry.runtime_data)])
|
2017-04-20 08:15:26 +02:00
|
|
|
|
|
|
|
|
|
2024-03-03 11:08:28 +01:00
|
|
|
class PingBinarySensor(PingEntity, BinarySensorEntity):
|
2017-04-20 08:15:26 +02:00
|
|
|
"""Representation of a Ping Binary sensor."""
|
|
|
|
|
|
2023-06-25 16:00:52 +02:00
|
|
|
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
|
2023-11-29 08:42:02 +01:00
|
|
|
_attr_available = False
|
2024-03-03 11:08:28 +01:00
|
|
|
_attr_name = None
|
2023-06-25 16:00:52 +02:00
|
|
|
|
2023-11-17 20:30:30 +01:00
|
|
|
def __init__(
|
2025-02-09 14:44:37 +01:00
|
|
|
self, config_entry: PingConfigEntry, coordinator: PingUpdateCoordinator
|
2023-11-17 20:30:30 +01:00
|
|
|
) -> None:
|
2017-04-20 08:15:26 +02:00
|
|
|
"""Initialize the Ping Binary sensor."""
|
2024-03-03 18:10:22 +01:00
|
|
|
super().__init__(config_entry, coordinator, config_entry.entry_id)
|
2023-11-17 20:30:30 +01:00
|
|
|
|
|
|
|
|
# if this was imported just enable it when it was enabled before
|
|
|
|
|
if CONF_IMPORTED_BY in config_entry.data:
|
|
|
|
|
self._attr_entity_registry_enabled_default = bool(
|
|
|
|
|
config_entry.data[CONF_IMPORTED_BY] == "binary_sensor"
|
|
|
|
|
)
|
|
|
|
|
|
2017-04-20 08:15:26 +02:00
|
|
|
@property
|
2020-08-19 00:25:50 +02:00
|
|
|
def is_on(self) -> bool:
|
2017-04-20 08:15:26 +02:00
|
|
|
"""Return true if the binary sensor is on."""
|
2023-11-18 17:07:58 +01:00
|
|
|
return self.coordinator.data.is_alive
|