forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add detailed status for UptimeRobot (home-assistant#64879)
Co-authored-by: Joakim Sørensen <[email protected]>
- Loading branch information
1 parent
eb5c607
commit 3f12ce0
Showing
10 changed files
with
178 additions
and
23 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
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""UptimeRobot sensor platform.""" | ||
from __future__ import annotations | ||
|
||
from typing import TypedDict | ||
|
||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity import EntityCategory | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from . import UptimeRobotDataUpdateCoordinator | ||
from .const import DOMAIN | ||
from .entity import UptimeRobotEntity | ||
|
||
|
||
class StatusValue(TypedDict): | ||
"""Sensor details.""" | ||
|
||
value: str | ||
icon: str | ||
|
||
|
||
SENSORS_INFO = { | ||
0: StatusValue(value="pause", icon="mdi:television-pause"), | ||
1: StatusValue(value="not_checked_yet", icon="mdi:television"), | ||
2: StatusValue(value="up", icon="mdi:television-shimmer"), | ||
8: StatusValue(value="seems_down", icon="mdi:television-off"), | ||
9: StatusValue(value="down", icon="mdi:television-off"), | ||
} | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up the UptimeRobot sensors.""" | ||
coordinator: UptimeRobotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] | ||
async_add_entities( | ||
[ | ||
UptimeRobotSensor( | ||
coordinator, | ||
SensorEntityDescription( | ||
key=str(monitor.id), | ||
name=monitor.friendly_name, | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
device_class="uptimerobot__monitor_status", | ||
), | ||
monitor=monitor, | ||
) | ||
for monitor in coordinator.data | ||
], | ||
) | ||
|
||
|
||
class UptimeRobotSensor(UptimeRobotEntity, SensorEntity): | ||
"""Representation of a UptimeRobot sensor.""" | ||
|
||
@property | ||
def native_value(self) -> str: | ||
"""Return the status of the monitor.""" | ||
return SENSORS_INFO[self.monitor.status]["value"] | ||
|
||
@property | ||
def icon(self) -> str: | ||
"""Return the status of the monitor.""" | ||
return SENSORS_INFO[self.monitor.status]["icon"] |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"state": { | ||
"uptimerobot__monitor_status": { | ||
"pause": "Pause", | ||
"not_checked_yet": "Not checked yet", | ||
"up": "Up", | ||
"seems_down": "Seems down", | ||
"down": "Down" | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
homeassistant/components/uptimerobot/translations/sensor.en.json
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"state": { | ||
"uptimerobot__monitor_status": { | ||
"down": "Down", | ||
"not_checked_yet": "Not checked yet", | ||
"pause": "Pause", | ||
"seems_down": "Seems down", | ||
"up": "Up" | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Test UptimeRobot sensor.""" | ||
|
||
from unittest.mock import patch | ||
|
||
from pyuptimerobot import UptimeRobotAuthenticationException | ||
|
||
from homeassistant.components.uptimerobot.const import COORDINATOR_UPDATE_INTERVAL | ||
from homeassistant.const import STATE_UNAVAILABLE | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.util import dt | ||
|
||
from .common import ( | ||
MOCK_UPTIMEROBOT_MONITOR, | ||
STATE_UP, | ||
UPTIMEROBOT_SENSOR_TEST_ENTITY, | ||
setup_uptimerobot_integration, | ||
) | ||
|
||
from tests.common import async_fire_time_changed | ||
|
||
SENSOR_ICON = "mdi:television-shimmer" | ||
|
||
|
||
async def test_presentation(hass: HomeAssistant) -> None: | ||
"""Test the presenstation of UptimeRobot sensors.""" | ||
await setup_uptimerobot_integration(hass) | ||
|
||
entity = hass.states.get(UPTIMEROBOT_SENSOR_TEST_ENTITY) | ||
|
||
assert entity.state == STATE_UP | ||
assert entity.attributes["icon"] == SENSOR_ICON | ||
assert entity.attributes["target"] == MOCK_UPTIMEROBOT_MONITOR["url"] | ||
|
||
|
||
async def test_unaviable_on_update_failure(hass: HomeAssistant) -> None: | ||
"""Test entity unaviable on update failure.""" | ||
await setup_uptimerobot_integration(hass) | ||
|
||
entity = hass.states.get(UPTIMEROBOT_SENSOR_TEST_ENTITY) | ||
assert entity.state == STATE_UP | ||
|
||
with patch( | ||
"pyuptimerobot.UptimeRobot.async_get_monitors", | ||
side_effect=UptimeRobotAuthenticationException, | ||
): | ||
async_fire_time_changed(hass, dt.utcnow() + COORDINATOR_UPDATE_INTERVAL) | ||
await hass.async_block_till_done() | ||
|
||
entity = hass.states.get(UPTIMEROBOT_SENSOR_TEST_ENTITY) | ||
assert entity.state == STATE_UNAVAILABLE |