-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from jayakornk/main
Rewrite and improve functionalities
- Loading branch information
Showing
14 changed files
with
407 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,60 @@ | ||
"""Summary binary data from Uptime Kuma.""" | ||
from voluptuous.validators import Boolean | ||
"""UptimeKuma binary_sensor platform.""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.components.binary_sensor import BinarySensorEntity | ||
from homeassistant.components.binary_sensor import ( | ||
BinarySensorDeviceClass, | ||
BinarySensorEntity, | ||
BinarySensorEntityDescription, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity import EntityDescription | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
from homeassistant.helpers.update_coordinator import ( | ||
CoordinatorEntity, | ||
DataUpdateCoordinator, | ||
) | ||
from pyuptimekuma import UptimeKumaMonitor | ||
|
||
from . import UptimeKumaDataUpdateCoordinator | ||
from .const import DOMAIN | ||
from .entity import UptimeKumaEntity | ||
from .utils import format_entity_name | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: ConfigEntry, | ||
entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Defer sensor setup to the shared sensor module.""" | ||
coordinator = hass.data[DOMAIN] | ||
"""Set up the UptimeKuma binary_sensors.""" | ||
coordinator: UptimeKumaDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] | ||
async_add_entities( | ||
UptimeKumaBinarySensor(coordinator, monitor) for monitor in coordinator.data | ||
UptimeKumaBinarySensor( | ||
coordinator, | ||
BinarySensorEntityDescription( | ||
key=str(monitor.monitor_name), | ||
name=monitor.monitor_name, | ||
device_class=BinarySensorDeviceClass.CONNECTIVITY, | ||
), | ||
monitor=monitor, | ||
) | ||
for monitor in coordinator.data | ||
) | ||
|
||
|
||
class UptimeKumaBinarySensor(BinarySensorEntity, CoordinatorEntity): | ||
"""Represents an Uptime Kuma binary sensor.""" | ||
|
||
_attr_icon = "mdi:cloud" | ||
|
||
def __init__(self, coordinator: DataUpdateCoordinator, monitor: str) -> None: | ||
"""Initialize the Uptime Kuma binary sensor.""" | ||
super().__init__(coordinator) | ||
class UptimeKumaBinarySensor(UptimeKumaEntity, BinarySensorEntity): | ||
"""Representation of a UptimeKuma binary sensor.""" | ||
|
||
self._attr_name = monitor | ||
def __init__( | ||
self, | ||
coordinator: UptimeKumaDataUpdateCoordinator, | ||
description: EntityDescription, | ||
monitor: UptimeKumaMonitor, | ||
) -> None: | ||
"""Set entity ID.""" | ||
super().__init__(coordinator, description, monitor) | ||
self.entity_id = ( | ||
f"binary_sensor.uptimekuma_{format_entity_name(self.monitor.monitor_name)}" | ||
) | ||
|
||
@property | ||
def is_on(self) -> Boolean: | ||
"""Return true if the binary sensor is on.""" | ||
return self.coordinator.data[self.name]["monitor_status"] == 1.0 | ||
def is_on(self) -> bool: | ||
"""Return True if the entity is on.""" | ||
return self.monitor_available |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
"""Constants for the Uptime Kuma integration.""" | ||
from __future__ import annotations | ||
|
||
from datetime import timedelta | ||
from logging import Logger, getLogger | ||
from typing import Final | ||
|
||
from homeassistant.const import Platform | ||
|
||
LOGGER: Logger = getLogger(__package__) | ||
|
||
# The free plan is limited to 10 requests/minute | ||
COORDINATOR_UPDATE_INTERVAL: timedelta = timedelta(seconds=10) | ||
|
||
DOMAIN = "uptime_kuma" | ||
PLATFORMS: Final = [Platform.BINARY_SENSOR, Platform.SENSOR] | ||
|
||
ATTRIBUTION: Final = "Data provided by Uptime Kuma" | ||
|
||
API_ATTR_OK: Final = "ok" |
Oops, something went wrong.