Add config flow for Ping (#103743)
This commit is contained in:
parent
2d891c77ef
commit
e5bc25523e
15 changed files with 473 additions and 211 deletions
|
@ -12,30 +12,26 @@ from homeassistant.components.binary_sensor import (
|
|||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME, STATE_ON
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from . import PingDomainData
|
||||
from .const import DOMAIN
|
||||
from .const import CONF_IMPORTED_BY, CONF_PING_COUNT, DEFAULT_PING_COUNT, DOMAIN
|
||||
from .helpers import PingDataICMPLib, PingDataSubProcess
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
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"
|
||||
|
||||
CONF_PING_COUNT = "count"
|
||||
|
||||
DEFAULT_NAME = "Ping"
|
||||
DEFAULT_PING_COUNT = 5
|
||||
|
||||
SCAN_INTERVAL = timedelta(minutes=5)
|
||||
|
||||
PARALLEL_UPDATES = 50
|
||||
|
@ -57,22 +53,49 @@ async def async_setup_platform(
|
|||
async_add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the Ping Binary sensor."""
|
||||
"""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."""
|
||||
|
||||
data: PingDomainData = hass.data[DOMAIN]
|
||||
|
||||
host: str = config[CONF_HOST]
|
||||
count: int = config[CONF_PING_COUNT]
|
||||
name: str = config.get(CONF_NAME, f"{DEFAULT_NAME} {host}")
|
||||
privileged: bool | None = data.privileged
|
||||
host: str = entry.options[CONF_HOST]
|
||||
count: int = int(entry.options[CONF_PING_COUNT])
|
||||
ping_cls: type[PingDataSubProcess | PingDataICMPLib]
|
||||
if privileged is None:
|
||||
if data.privileged is None:
|
||||
ping_cls = PingDataSubProcess
|
||||
else:
|
||||
ping_cls = PingDataICMPLib
|
||||
|
||||
async_add_entities(
|
||||
[PingBinarySensor(name, ping_cls(hass, host, count, privileged))]
|
||||
[PingBinarySensor(entry, ping_cls(hass, host, count, data.privileged))]
|
||||
)
|
||||
|
||||
|
||||
|
@ -80,12 +103,24 @@ class PingBinarySensor(RestoreEntity, BinarySensorEntity):
|
|||
"""Representation of a Ping Binary sensor."""
|
||||
|
||||
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
|
||||
_attr_available = False
|
||||
|
||||
def __init__(self, name: str, ping: PingDataSubProcess | PingDataICMPLib) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
config_entry: ConfigEntry,
|
||||
ping_cls: PingDataSubProcess | PingDataICMPLib,
|
||||
) -> None:
|
||||
"""Initialize the Ping Binary sensor."""
|
||||
self._attr_available = False
|
||||
self._attr_name = name
|
||||
self._ping = ping
|
||||
self._attr_name = config_entry.title
|
||||
self._attr_unique_id = config_entry.entry_id
|
||||
|
||||
# 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"
|
||||
)
|
||||
|
||||
self._ping = ping_cls
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue