2017-10-14 18:06:44 +00:00
|
|
|
"""The tests for the uptime sensor platform."""
|
2019-10-01 14:59:06 +00:00
|
|
|
import asyncio
|
2019-12-09 18:07:32 +00:00
|
|
|
from datetime import timedelta
|
2017-10-14 18:06:44 +00:00
|
|
|
import unittest
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
2019-03-19 06:07:39 +00:00
|
|
|
from homeassistant.components.uptime.sensor import UptimeSensor
|
2019-12-09 18:07:32 +00:00
|
|
|
from homeassistant.setup import setup_component
|
|
|
|
|
2017-10-14 18:06:44 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
|
|
|
|
|
|
|
class TestUptimeSensor(unittest.TestCase):
|
|
|
|
"""Test the uptime sensor."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Set up things to run when tests begin."""
|
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_uptime_min_config(self):
|
2017-10-24 12:44:38 +00:00
|
|
|
"""Test minimum uptime configuration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"sensor": {"platform": "uptime"}}
|
|
|
|
assert setup_component(self.hass, "sensor", config)
|
2017-10-14 18:06:44 +00:00
|
|
|
|
|
|
|
def test_uptime_sensor_name_change(self):
|
|
|
|
"""Test uptime sensor with different name."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"sensor": {"platform": "uptime", "name": "foobar"}}
|
|
|
|
assert setup_component(self.hass, "sensor", config)
|
2017-10-14 18:06:44 +00:00
|
|
|
|
|
|
|
def test_uptime_sensor_config_hours(self):
|
|
|
|
"""Test uptime sensor with hours defined in config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"sensor": {"platform": "uptime", "unit_of_measurement": "hours"}}
|
|
|
|
assert setup_component(self.hass, "sensor", config)
|
2017-10-14 18:06:44 +00:00
|
|
|
|
2017-10-23 18:38:16 +00:00
|
|
|
def test_uptime_sensor_config_minutes(self):
|
|
|
|
"""Test uptime sensor with minutes defined in config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
config = {"sensor": {"platform": "uptime", "unit_of_measurement": "minutes"}}
|
|
|
|
assert setup_component(self.hass, "sensor", config)
|
2017-10-23 18:38:16 +00:00
|
|
|
|
2017-10-14 18:06:44 +00:00
|
|
|
def test_uptime_sensor_days_output(self):
|
|
|
|
"""Test uptime sensor output data."""
|
2019-07-31 19:25:30 +00:00
|
|
|
sensor = UptimeSensor("test", "days")
|
|
|
|
assert sensor.unit_of_measurement == "days"
|
2017-10-14 18:06:44 +00:00
|
|
|
new_time = sensor.initial + timedelta(days=1)
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.now", return_value=new_time):
|
2019-10-01 14:59:06 +00:00
|
|
|
asyncio.run_coroutine_threadsafe(
|
|
|
|
sensor.async_update(), self.hass.loop
|
|
|
|
).result()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert sensor.state == 1.00
|
2017-10-14 18:06:44 +00:00
|
|
|
new_time = sensor.initial + timedelta(days=111.499)
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.now", return_value=new_time):
|
2019-10-01 14:59:06 +00:00
|
|
|
asyncio.run_coroutine_threadsafe(
|
|
|
|
sensor.async_update(), self.hass.loop
|
|
|
|
).result()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert sensor.state == 111.50
|
2017-10-14 18:06:44 +00:00
|
|
|
|
|
|
|
def test_uptime_sensor_hours_output(self):
|
|
|
|
"""Test uptime sensor output data."""
|
2019-07-31 19:25:30 +00:00
|
|
|
sensor = UptimeSensor("test", "hours")
|
|
|
|
assert sensor.unit_of_measurement == "hours"
|
2017-10-14 18:06:44 +00:00
|
|
|
new_time = sensor.initial + timedelta(hours=16)
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.now", return_value=new_time):
|
2019-10-01 14:59:06 +00:00
|
|
|
asyncio.run_coroutine_threadsafe(
|
|
|
|
sensor.async_update(), self.hass.loop
|
|
|
|
).result()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert sensor.state == 16.00
|
2017-10-14 18:06:44 +00:00
|
|
|
new_time = sensor.initial + timedelta(hours=72.499)
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.now", return_value=new_time):
|
2019-10-01 14:59:06 +00:00
|
|
|
asyncio.run_coroutine_threadsafe(
|
|
|
|
sensor.async_update(), self.hass.loop
|
|
|
|
).result()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert sensor.state == 72.50
|
2017-10-23 18:38:16 +00:00
|
|
|
|
|
|
|
def test_uptime_sensor_minutes_output(self):
|
|
|
|
"""Test uptime sensor output data."""
|
2019-07-31 19:25:30 +00:00
|
|
|
sensor = UptimeSensor("test", "minutes")
|
|
|
|
assert sensor.unit_of_measurement == "minutes"
|
2017-10-23 18:38:16 +00:00
|
|
|
new_time = sensor.initial + timedelta(minutes=16)
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.now", return_value=new_time):
|
2019-10-01 14:59:06 +00:00
|
|
|
asyncio.run_coroutine_threadsafe(
|
|
|
|
sensor.async_update(), self.hass.loop
|
|
|
|
).result()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert sensor.state == 16.00
|
2017-10-23 18:38:16 +00:00
|
|
|
new_time = sensor.initial + timedelta(minutes=12.499)
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.dt.now", return_value=new_time):
|
2019-10-01 14:59:06 +00:00
|
|
|
asyncio.run_coroutine_threadsafe(
|
|
|
|
sensor.async_update(), self.hass.loop
|
|
|
|
).result()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert sensor.state == 12.50
|