2019-04-03 15:40:03 +00:00
|
|
|
"""Tracks the latency of a host by sending ICMP echo requests (ping)."""
|
2021-03-18 12:21:46 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-04-20 06:15:26 +00:00
|
|
|
import logging
|
2023-10-22 08:12:59 +00:00
|
|
|
from typing import Any
|
2017-04-20 06:15:26 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-09-12 18:21:57 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2023-06-25 14:00:52 +00:00
|
|
|
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
2021-12-16 14:38:02 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-12 18:21:57 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2023-11-17 19:30:30 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2023-11-18 16:07:58 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
2023-11-17 19:30:30 +00:00
|
|
|
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
2018-10-02 09:43:34 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-07 15:28:24 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-11-17 19:30:30 +00:00
|
|
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
2022-01-07 15:28:24 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2023-11-29 07:42:02 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2020-08-05 10:43:35 +00:00
|
|
|
|
2023-10-20 21:46:33 +00:00
|
|
|
from . import PingDomainData
|
2023-11-17 19:30:30 +00:00
|
|
|
from .const import CONF_IMPORTED_BY, CONF_PING_COUNT, DEFAULT_PING_COUNT, DOMAIN
|
2023-11-18 16:07:58 +00:00
|
|
|
from .coordinator import PingUpdateCoordinator
|
2017-04-20 06:15:26 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ROUND_TRIP_TIME_AVG = "round_trip_time_avg"
|
|
|
|
ATTR_ROUND_TRIP_TIME_MAX = "round_trip_time_max"
|
|
|
|
ATTR_ROUND_TRIP_TIME_MDEV = "round_trip_time_mdev"
|
|
|
|
ATTR_ROUND_TRIP_TIME_MIN = "round_trip_time_min"
|
2017-04-20 06:15:26 +00:00
|
|
|
|
2023-06-25 14:00:52 +00:00
|
|
|
PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
2020-08-05 10:43:35 +00:00
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_PING_COUNT, default=DEFAULT_PING_COUNT): vol.Range(
|
|
|
|
min=1, max=100
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2017-04-20 06:15:26 +00:00
|
|
|
|
|
|
|
|
2021-03-31 13:06:49 +00:00
|
|
|
async def async_setup_platform(
|
2022-01-07 15:28:24 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
2021-03-31 13:06:49 +00:00
|
|
|
) -> None:
|
2023-11-17 19:30:30 +00:00
|
|
|
"""YAML init: import via config flow."""
|
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data={CONF_IMPORTED_BY: "binary_sensor", **config},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
async_create_issue(
|
|
|
|
hass,
|
|
|
|
HOMEASSISTANT_DOMAIN,
|
|
|
|
f"deprecated_yaml_{DOMAIN}",
|
|
|
|
breaks_in_ha_version="2024.6.0",
|
|
|
|
is_fixable=False,
|
|
|
|
issue_domain=DOMAIN,
|
|
|
|
severity=IssueSeverity.WARNING,
|
|
|
|
translation_key="deprecated_yaml",
|
|
|
|
translation_placeholders={
|
|
|
|
"domain": DOMAIN,
|
|
|
|
"integration_title": "Ping",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Set up a Ping config entry."""
|
2023-10-20 21:46:33 +00:00
|
|
|
|
|
|
|
data: PingDomainData = hass.data[DOMAIN]
|
|
|
|
|
2023-11-18 16:07:58 +00:00
|
|
|
async_add_entities([PingBinarySensor(entry, data.coordinators[entry.entry_id])])
|
2017-04-20 06:15:26 +00:00
|
|
|
|
|
|
|
|
2023-11-29 07:42:02 +00:00
|
|
|
class PingBinarySensor(CoordinatorEntity[PingUpdateCoordinator], BinarySensorEntity):
|
2017-04-20 06:15:26 +00:00
|
|
|
"""Representation of a Ping Binary sensor."""
|
|
|
|
|
2023-06-25 14:00:52 +00:00
|
|
|
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
|
2023-11-29 07:42:02 +00:00
|
|
|
_attr_available = False
|
2023-06-25 14:00:52 +00:00
|
|
|
|
2023-11-17 19:30:30 +00:00
|
|
|
def __init__(
|
2023-11-18 16:07:58 +00:00
|
|
|
self, config_entry: ConfigEntry, coordinator: PingUpdateCoordinator
|
2023-11-17 19:30:30 +00:00
|
|
|
) -> None:
|
2017-04-20 06:15:26 +00:00
|
|
|
"""Initialize the Ping Binary sensor."""
|
2023-11-29 07:42:02 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
|
|
|
|
self._attr_name = config_entry.title
|
|
|
|
self._attr_unique_id = config_entry.entry_id
|
2023-11-17 19:30:30 +00: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 06:15:26 +00:00
|
|
|
@property
|
2020-08-18 22:25:50 +00:00
|
|
|
def is_on(self) -> bool:
|
2017-04-20 06:15:26 +00:00
|
|
|
"""Return true if the binary sensor is on."""
|
2023-11-18 16:07:58 +00:00
|
|
|
return self.coordinator.data.is_alive
|
2017-04-20 06:15:26 +00:00
|
|
|
|
|
|
|
@property
|
2022-01-19 19:59:20 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
2023-11-29 07:42:02 +00:00
|
|
|
"""Return the state attributes of the ICMP checo request."""
|
2023-11-18 16:07:58 +00:00
|
|
|
if self.coordinator.data.data is None:
|
2022-01-19 19:59:20 +00:00
|
|
|
return None
|
|
|
|
return {
|
2023-11-18 16:07:58 +00:00
|
|
|
ATTR_ROUND_TRIP_TIME_AVG: self.coordinator.data.data["avg"],
|
|
|
|
ATTR_ROUND_TRIP_TIME_MAX: self.coordinator.data.data["max"],
|
|
|
|
ATTR_ROUND_TRIP_TIME_MDEV: self.coordinator.data.data["mdev"],
|
|
|
|
ATTR_ROUND_TRIP_TIME_MIN: self.coordinator.data.data["min"],
|
2021-03-31 13:06:49 +00:00
|
|
|
}
|