core/tests/components/uptime/test_sensor.py

94 lines
3.8 KiB
Python
Raw Normal View History

"""The tests for the uptime sensor platform."""
import asyncio
from datetime import timedelta
import unittest
from unittest.mock import patch
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
from homeassistant.components.uptime.sensor import UptimeSensor
from homeassistant.setup import setup_component
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)
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)
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-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
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"
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):
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 1.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):
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 111.50
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"
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):
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 16.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):
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
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):
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
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):
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 12.50