2019-04-03 15:40:03 +00:00
|
|
|
"""A platform that to monitor Uptime Robot monitors."""
|
2021-08-04 20:20:03 +00:00
|
|
|
from __future__ import annotations
|
2018-06-10 08:28:53 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-09-12 18:21:57 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
DEVICE_CLASS_CONNECTIVITY,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
BinarySensorEntity,
|
2021-08-03 18:20:12 +00:00
|
|
|
BinarySensorEntityDescription,
|
2020-09-12 18:21:57 +00:00
|
|
|
)
|
2021-08-04 20:20:03 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|
|
|
from homeassistant.const import CONF_API_KEY
|
2021-08-03 18:20:12 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-06-10 08:28:53 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-08-04 20:20:03 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2018-06-10 08:28:53 +00:00
|
|
|
|
2021-08-04 20:20:03 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
from .entity import UptimeRobotEntity
|
2018-06-10 08:58:45 +00:00
|
|
|
|
2021-08-04 20:20:03 +00:00
|
|
|
PLATFORM_SCHEMA = cv.deprecated(
|
|
|
|
vol.All(PLATFORM_SCHEMA.extend({vol.Required(CONF_API_KEY): cv.string}))
|
|
|
|
)
|
2018-06-10 08:28:53 +00:00
|
|
|
|
|
|
|
|
2021-08-03 18:20:12 +00:00
|
|
|
async def async_setup_platform(
|
2021-08-04 20:20:03 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Uptime Robot binary_sensor platform."""
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
|
|
|
|
)
|
2021-08-03 18:20:12 +00:00
|
|
|
)
|
2018-06-10 08:28:53 +00:00
|
|
|
|
|
|
|
|
2021-08-04 20:20:03 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Uptime Robot binary_sensors."""
|
|
|
|
coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2021-08-03 18:20:12 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
2019-07-31 19:25:30 +00:00
|
|
|
UptimeRobotBinarySensor(
|
2021-08-03 18:20:12 +00:00
|
|
|
coordinator,
|
2021-08-04 07:29:51 +00:00
|
|
|
BinarySensorEntityDescription(
|
2021-08-04 20:20:03 +00:00
|
|
|
key=str(monitor.id),
|
2021-08-05 10:13:47 +00:00
|
|
|
name=monitor.friendly_name,
|
2021-08-03 18:20:12 +00:00
|
|
|
device_class=DEVICE_CLASS_CONNECTIVITY,
|
|
|
|
),
|
2021-08-04 20:20:03 +00:00
|
|
|
target=monitor.url,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-08-04 20:20:03 +00:00
|
|
|
for monitor in coordinator.data
|
2021-08-03 18:20:12 +00:00
|
|
|
],
|
|
|
|
)
|
2018-06-10 08:28:53 +00:00
|
|
|
|
|
|
|
|
2021-08-04 20:20:03 +00:00
|
|
|
class UptimeRobotBinarySensor(UptimeRobotEntity, BinarySensorEntity):
|
2018-06-10 08:58:45 +00:00
|
|
|
"""Representation of a Uptime Robot binary sensor."""
|
2018-06-10 08:28:53 +00:00
|
|
|
|
2021-08-04 07:29:51 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return True if the entity is on."""
|
2021-08-04 20:20:03 +00:00
|
|
|
return self.monitor_available
|