mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
Move uptime from relative time to absolute time (#43623)
This commit is contained in:
parent
00d0c3f98b
commit
52217f1f60
@ -1,47 +1,42 @@
|
|||||||
"""Platform to retrieve uptime for Home Assistant."""
|
"""Platform to retrieve uptime for Home Assistant."""
|
||||||
import logging
|
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import DEVICE_CLASS_TIMESTAMP, PLATFORM_SCHEMA
|
||||||
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
|
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
DEFAULT_NAME = "Uptime"
|
DEFAULT_NAME = "Uptime"
|
||||||
|
|
||||||
ICON = "mdi:clock"
|
PLATFORM_SCHEMA = vol.All(
|
||||||
|
cv.deprecated(CONF_UNIT_OF_MEASUREMENT),
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
vol.Optional(CONF_UNIT_OF_MEASUREMENT, default="days"): vol.All(
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT, default="days"): vol.All(
|
||||||
cv.string, vol.In(["minutes", "hours", "days", "seconds"])
|
cv.string, vol.In(["minutes", "hours", "days", "seconds"])
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||||
"""Set up the uptime sensor platform."""
|
"""Set up the uptime sensor platform."""
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
units = config.get(CONF_UNIT_OF_MEASUREMENT)
|
|
||||||
|
|
||||||
async_add_entities([UptimeSensor(name, units)], True)
|
async_add_entities([UptimeSensor(name)], True)
|
||||||
|
|
||||||
|
|
||||||
class UptimeSensor(Entity):
|
class UptimeSensor(Entity):
|
||||||
"""Representation of an uptime sensor."""
|
"""Representation of an uptime sensor."""
|
||||||
|
|
||||||
def __init__(self, name, unit):
|
def __init__(self, name):
|
||||||
"""Initialize the uptime sensor."""
|
"""Initialize the uptime sensor."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._unit = unit
|
self._state = dt_util.now().isoformat()
|
||||||
self.initial = dt_util.now()
|
|
||||||
self._state = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -49,32 +44,16 @@ class UptimeSensor(Entity):
|
|||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def device_class(self):
|
||||||
"""Icon to display in the front end."""
|
"""Return device class."""
|
||||||
return ICON
|
return DEVICE_CLASS_TIMESTAMP
|
||||||
|
|
||||||
@property
|
|
||||||
def unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement the value is expressed in."""
|
|
||||||
return self._unit
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
async def async_update(self):
|
@property
|
||||||
"""Update the state of the sensor."""
|
def should_poll(self) -> bool:
|
||||||
delta = dt_util.now() - self.initial
|
"""Disable polling for this entity."""
|
||||||
div_factor = 3600
|
return False
|
||||||
|
|
||||||
if self.unit_of_measurement == "days":
|
|
||||||
div_factor *= 24
|
|
||||||
elif self.unit_of_measurement == "minutes":
|
|
||||||
div_factor /= 60
|
|
||||||
elif self.unit_of_measurement == "seconds":
|
|
||||||
div_factor /= 3600
|
|
||||||
|
|
||||||
delta = delta.total_seconds() / div_factor
|
|
||||||
self._state = round(delta, 2)
|
|
||||||
_LOGGER.debug("New value: %s", delta)
|
|
||||||
|
@ -1,85 +1,11 @@
|
|||||||
"""The tests for the uptime sensor platform."""
|
"""The tests for the uptime sensor platform."""
|
||||||
from datetime import timedelta
|
|
||||||
|
|
||||||
from homeassistant.components.uptime.sensor import UptimeSensor
|
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.async_mock import patch
|
|
||||||
|
|
||||||
|
|
||||||
async def test_uptime_min_config(hass):
|
|
||||||
"""Test minimum uptime configuration."""
|
|
||||||
config = {"sensor": {"platform": "uptime"}}
|
|
||||||
assert await async_setup_component(hass, "sensor", config)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
state = hass.states.get("sensor.uptime")
|
|
||||||
assert state.attributes.get("unit_of_measurement") == "days"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_uptime_sensor_name_change(hass):
|
async def test_uptime_sensor_name_change(hass):
|
||||||
"""Test uptime sensor with different name."""
|
"""Test uptime sensor with different name."""
|
||||||
config = {"sensor": {"platform": "uptime", "name": "foobar"}}
|
config = {"sensor": {"platform": "uptime", "name": "foobar"}}
|
||||||
assert await async_setup_component(hass, "sensor", config)
|
assert await async_setup_component(hass, "sensor", config)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get("sensor.foobar")
|
assert hass.states.get("sensor.foobar")
|
||||||
assert state.attributes.get("unit_of_measurement") == "days"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_uptime_sensor_config_hours(hass):
|
|
||||||
"""Test uptime sensor with hours defined in config."""
|
|
||||||
config = {"sensor": {"platform": "uptime", "unit_of_measurement": "hours"}}
|
|
||||||
assert await async_setup_component(hass, "sensor", config)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
state = hass.states.get("sensor.uptime")
|
|
||||||
assert state.attributes.get("unit_of_measurement") == "hours"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_uptime_sensor_config_minutes(hass):
|
|
||||||
"""Test uptime sensor with minutes defined in config."""
|
|
||||||
config = {"sensor": {"platform": "uptime", "unit_of_measurement": "minutes"}}
|
|
||||||
assert await async_setup_component(hass, "sensor", config)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
state = hass.states.get("sensor.uptime")
|
|
||||||
assert state.attributes.get("unit_of_measurement") == "minutes"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_uptime_sensor_days_output(hass):
|
|
||||||
"""Test uptime sensor output data."""
|
|
||||||
sensor = UptimeSensor("test", "days")
|
|
||||||
assert sensor.unit_of_measurement == "days"
|
|
||||||
new_time = sensor.initial + timedelta(days=1)
|
|
||||||
with patch("homeassistant.util.dt.now", return_value=new_time):
|
|
||||||
await sensor.async_update()
|
|
||||||
assert sensor.state == 1.00
|
|
||||||
new_time = sensor.initial + timedelta(days=111.499)
|
|
||||||
with patch("homeassistant.util.dt.now", return_value=new_time):
|
|
||||||
await sensor.async_update()
|
|
||||||
assert sensor.state == 111.50
|
|
||||||
|
|
||||||
|
|
||||||
async def test_uptime_sensor_hours_output(hass):
|
|
||||||
"""Test uptime sensor output data."""
|
|
||||||
sensor = UptimeSensor("test", "hours")
|
|
||||||
assert sensor.unit_of_measurement == "hours"
|
|
||||||
new_time = sensor.initial + timedelta(hours=16)
|
|
||||||
with patch("homeassistant.util.dt.now", return_value=new_time):
|
|
||||||
await sensor.async_update()
|
|
||||||
assert sensor.state == 16.00
|
|
||||||
new_time = sensor.initial + timedelta(hours=72.499)
|
|
||||||
with patch("homeassistant.util.dt.now", return_value=new_time):
|
|
||||||
await sensor.async_update()
|
|
||||||
assert sensor.state == 72.50
|
|
||||||
|
|
||||||
|
|
||||||
async def test_uptime_sensor_minutes_output(hass):
|
|
||||||
"""Test uptime sensor output data."""
|
|
||||||
sensor = UptimeSensor("test", "minutes")
|
|
||||||
assert sensor.unit_of_measurement == "minutes"
|
|
||||||
new_time = sensor.initial + timedelta(minutes=16)
|
|
||||||
with patch("homeassistant.util.dt.now", return_value=new_time):
|
|
||||||
await sensor.async_update()
|
|
||||||
assert sensor.state == 16.00
|
|
||||||
new_time = sensor.initial + timedelta(minutes=12.499)
|
|
||||||
with patch("homeassistant.util.dt.now", return_value=new_time):
|
|
||||||
await sensor.async_update()
|
|
||||||
assert sensor.state == 12.50
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user