Cleanup coroutine threadsafe (#27080)

* Cleanup coroutine threadsafe

* fix lint

* Fix typing

* Fix tests

* Fix black
This commit is contained in:
Pascal Vizeli authored and GitHub committed 2019-10-01 16:59:06 +02:00
1 parent f4a1f2809b
commit c1851a2d94
23 files changed
+196 -232

No files matched your search

+19 -7
View File
@@ -1,9 +1,9 @@
"""The tests for the uptime sensor platform."""
import asyncio
import unittest
from unittest.mock import patch
from datetime import timedelta
from homeassistant.util.async_ import run_coroutine_threadsafe
from homeassistant.setup import setup_component
from homeassistant.components.uptime.sensor import UptimeSensor
from tests.common import get_test_home_assistant
@@ -46,11 +46,15 @@ class TestUptimeSensor(unittest.TestCase):
assert sensor.unit_of_measurement == "days"
new_time = sensor.initial + timedelta(days=1)
with patch("homeassistant.util.dt.now", return_value=new_time):
run_coroutine_threadsafe(sensor.async_update(), self.hass.loop).result()
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 1.00
new_time = sensor.initial + timedelta(days=111.499)
with patch("homeassistant.util.dt.now", return_value=new_time):
run_coroutine_threadsafe(sensor.async_update(), self.hass.loop).result()
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 111.50
def test_uptime_sensor_hours_output(self):
@@ -59,11 +63,15 @@ class TestUptimeSensor(unittest.TestCase):
assert sensor.unit_of_measurement == "hours"
new_time = sensor.initial + timedelta(hours=16)
with patch("homeassistant.util.dt.now", return_value=new_time):
run_coroutine_threadsafe(sensor.async_update(), self.hass.loop).result()
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 16.00
new_time = sensor.initial + timedelta(hours=72.499)
with patch("homeassistant.util.dt.now", return_value=new_time):
run_coroutine_threadsafe(sensor.async_update(), self.hass.loop).result()
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 72.50
def test_uptime_sensor_minutes_output(self):
@@ -72,9 +80,13 @@ class TestUptimeSensor(unittest.TestCase):
assert sensor.unit_of_measurement == "minutes"
new_time = sensor.initial + timedelta(minutes=16)
with patch("homeassistant.util.dt.now", return_value=new_time):
run_coroutine_threadsafe(sensor.async_update(), self.hass.loop).result()
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 16.00
new_time = sensor.initial + timedelta(minutes=12.499)
with patch("homeassistant.util.dt.now", return_value=new_time):
run_coroutine_threadsafe(sensor.async_update(), self.hass.loop).result()
asyncio.run_coroutine_threadsafe(
sensor.async_update(), self.hass.loop
).result()
assert sensor.state == 12.50