diff --git a/homeassistant/components/ping/binary_sensor.py b/homeassistant/components/ping/binary_sensor.py index 71950887285..97e2aff7bff 100644 --- a/homeassistant/components/ping/binary_sensor.py +++ b/homeassistant/components/ping/binary_sensor.py @@ -74,6 +74,7 @@ async def async_setup_platform( count = config[CONF_PING_COUNT] name = config.get(CONF_NAME, f"{DEFAULT_NAME} {host}") privileged = hass.data[DOMAIN][PING_PRIVS] + ping_cls: type[PingDataSubProcess | PingDataICMPLib] if privileged is None: ping_cls = PingDataSubProcess else: @@ -87,7 +88,7 @@ async def async_setup_platform( class PingBinarySensor(RestoreEntity, BinarySensorEntity): """Representation of a Ping Binary sensor.""" - def __init__(self, name: str, ping) -> None: + def __init__(self, name: str, ping: PingDataSubProcess | PingDataICMPLib) -> None: """Initialize the Ping Binary sensor.""" self._available = False self._name = name @@ -99,7 +100,7 @@ class PingBinarySensor(RestoreEntity, BinarySensorEntity): return self._name @property - def available(self) -> str: + def available(self) -> bool: """Return if we have done the first ping.""" return self._available @@ -114,15 +115,16 @@ class PingBinarySensor(RestoreEntity, BinarySensorEntity): return self._ping.is_alive @property - def extra_state_attributes(self) -> dict[str, Any]: + def extra_state_attributes(self) -> dict[str, Any] | None: """Return the state attributes of the ICMP checo request.""" - if self._ping.data is not False: - return { - ATTR_ROUND_TRIP_TIME_AVG: self._ping.data["avg"], - ATTR_ROUND_TRIP_TIME_MAX: self._ping.data["max"], - ATTR_ROUND_TRIP_TIME_MDEV: self._ping.data["mdev"], - ATTR_ROUND_TRIP_TIME_MIN: self._ping.data["min"], - } + if self._ping.data is None: + return None + return { + ATTR_ROUND_TRIP_TIME_AVG: self._ping.data["avg"], + ATTR_ROUND_TRIP_TIME_MAX: self._ping.data["max"], + ATTR_ROUND_TRIP_TIME_MDEV: self._ping.data["mdev"], + ATTR_ROUND_TRIP_TIME_MIN: self._ping.data["min"], + } async def async_update(self) -> None: """Get the latest data.""" @@ -159,7 +161,7 @@ class PingData: self.hass = hass self._ip_address = host self._count = count - self.data = {} + self.data: dict[str, Any] | None = None self.is_alive = False @@ -187,7 +189,7 @@ class PingDataICMPLib(PingData): self.is_alive = data.is_alive if not self.is_alive: - self.data = False + self.data = None return self.data = { @@ -270,11 +272,11 @@ class PingDataSubProcess(PingData): await pinger.kill() del pinger - return False + return None except AttributeError: - return False + return None async def async_update(self) -> None: """Retrieve the latest details from the host.""" self.data = await self.async_ping() - self.is_alive = bool(self.data) + self.is_alive = self.data is not None diff --git a/homeassistant/components/ping/device_tracker.py b/homeassistant/components/ping/device_tracker.py index 3357d332f93..0a0e397e6d8 100644 --- a/homeassistant/components/ping/device_tracker.py +++ b/homeassistant/components/ping/device_tracker.py @@ -11,7 +11,9 @@ from icmplib import async_multiping import voluptuous as vol from homeassistant import const, util -from homeassistant.components.device_tracker import PLATFORM_SCHEMA +from homeassistant.components.device_tracker import ( + PLATFORM_SCHEMA as BASE_PLATFORM_SCHEMA, +) from homeassistant.components.device_tracker.const import ( CONF_SCAN_INTERVAL, SCAN_INTERVAL, @@ -32,7 +34,7 @@ PARALLEL_UPDATES = 0 CONF_PING_COUNT = "count" CONCURRENT_PING_LIMIT = 6 -PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( +PLATFORM_SCHEMA = BASE_PLATFORM_SCHEMA.extend( { vol.Required(const.CONF_HOSTS): {cv.slug: cv.string}, vol.Optional(CONF_PING_COUNT, default=1): cv.positive_int, diff --git a/mypy.ini b/mypy.ini index 3fc3269b641..983b10c9eb6 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2155,9 +2155,6 @@ ignore_errors = true [mypy-homeassistant.components.philips_js.*] ignore_errors = true -[mypy-homeassistant.components.ping.*] -ignore_errors = true - [mypy-homeassistant.components.pioneer.*] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index c711e6ad094..1570579a605 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -60,7 +60,6 @@ IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.onvif.*", "homeassistant.components.ozw.*", "homeassistant.components.philips_js.*", - "homeassistant.components.ping.*", "homeassistant.components.pioneer.*", "homeassistant.components.plaato.*", "homeassistant.components.plex.*",