mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Address review comments for 53918 (#53927)
This commit is contained in:
parent
083868ac01
commit
f9071a40de
@ -1,5 +1,4 @@
|
|||||||
"""A platform that to monitor Uptime Robot monitors."""
|
"""A platform that to monitor Uptime Robot monitors."""
|
||||||
from dataclasses import dataclass
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -32,13 +31,6 @@ ATTRIBUTION = "Data provided by Uptime Robot"
|
|||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_API_KEY): cv.string})
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_API_KEY): cv.string})
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class UptimeRobotBinarySensorEntityDescription(BinarySensorEntityDescription):
|
|
||||||
"""Entity description for UptimeRobotBinarySensor."""
|
|
||||||
|
|
||||||
target: str = ""
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
hass: HomeAssistant, config, async_add_entities, discovery_info=None
|
hass: HomeAssistant, config, async_add_entities, discovery_info=None
|
||||||
):
|
):
|
||||||
@ -46,12 +38,11 @@ async def async_setup_platform(
|
|||||||
uptime_robot_api = UptimeRobot()
|
uptime_robot_api = UptimeRobot()
|
||||||
api_key = config[CONF_API_KEY]
|
api_key = config[CONF_API_KEY]
|
||||||
|
|
||||||
|
def api_wrapper():
|
||||||
|
return uptime_robot_api.getMonitors(api_key)
|
||||||
|
|
||||||
async def async_update_data():
|
async def async_update_data():
|
||||||
"""Fetch data from API UptimeRobot API."""
|
"""Fetch data from API UptimeRobot API."""
|
||||||
|
|
||||||
def api_wrapper():
|
|
||||||
return uptime_robot_api.getMonitors(api_key)
|
|
||||||
|
|
||||||
async with async_timeout.timeout(10):
|
async with async_timeout.timeout(10):
|
||||||
monitors = await hass.async_add_executor_job(api_wrapper)
|
monitors = await hass.async_add_executor_job(api_wrapper)
|
||||||
if not monitors or monitors.get("stat") != "ok":
|
if not monitors or monitors.get("stat") != "ok":
|
||||||
@ -76,16 +67,15 @@ async def async_setup_platform(
|
|||||||
[
|
[
|
||||||
UptimeRobotBinarySensor(
|
UptimeRobotBinarySensor(
|
||||||
coordinator,
|
coordinator,
|
||||||
UptimeRobotBinarySensorEntityDescription(
|
BinarySensorEntityDescription(
|
||||||
key=monitor["id"],
|
key=monitor["id"],
|
||||||
name=monitor["friendly_name"],
|
name=monitor["friendly_name"],
|
||||||
target=monitor["url"],
|
|
||||||
device_class=DEVICE_CLASS_CONNECTIVITY,
|
device_class=DEVICE_CLASS_CONNECTIVITY,
|
||||||
),
|
),
|
||||||
|
target=monitor["url"],
|
||||||
)
|
)
|
||||||
for monitor in coordinator.data["monitors"]
|
for monitor in coordinator.data["monitors"]
|
||||||
],
|
],
|
||||||
True,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -95,28 +85,28 @@ class UptimeRobotBinarySensor(BinarySensorEntity, CoordinatorEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: DataUpdateCoordinator,
|
coordinator: DataUpdateCoordinator,
|
||||||
description: UptimeRobotBinarySensorEntityDescription,
|
description: BinarySensorEntityDescription,
|
||||||
|
target: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize Uptime Robot the binary sensor."""
|
"""Initialize Uptime Robot the binary sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self.coordinator = coordinator
|
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
|
self._target = target
|
||||||
self._attr_extra_state_attributes = {
|
self._attr_extra_state_attributes = {
|
||||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||||
ATTR_TARGET: self.entity_description.target,
|
ATTR_TARGET: self._target,
|
||||||
}
|
}
|
||||||
|
|
||||||
async def async_update(self):
|
@property
|
||||||
"""Get the latest state of the binary sensor."""
|
def is_on(self) -> bool:
|
||||||
if monitor := get_monitor_by_id(
|
"""Return True if the entity is on."""
|
||||||
self.coordinator.data.get("monitors", []), self.entity_description.key
|
if monitor := next(
|
||||||
|
(
|
||||||
|
monitor
|
||||||
|
for monitor in self.coordinator.data.get("monitors", [])
|
||||||
|
if monitor["id"] == self.entity_description.key
|
||||||
|
),
|
||||||
|
None,
|
||||||
):
|
):
|
||||||
self._attr_is_on = monitor["status"] == 2
|
return monitor["status"] == 2
|
||||||
|
return False
|
||||||
|
|
||||||
def get_monitor_by_id(monitors, monitor_id):
|
|
||||||
"""Return the monitor object matching the id."""
|
|
||||||
filtered = [monitor for monitor in monitors if monitor["id"] == monitor_id]
|
|
||||||
if len(filtered) == 0:
|
|
||||||
return
|
|
||||||
return filtered[0]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user