Cleanup coroutine threadsafe (#27080)
* Cleanup coroutine threadsafe * fix lint * Fix typing * Fix tests * Fix black
This commit is contained in:
1 parent
f4a1f2809b
commit
c1851a2d94
23 files changed
+196
-232
No files matched your search
@@ -17,7 +17,6 @@ from homeassistant.components.camera.const import DOMAIN, PREF_PRELOAD_STREAM
|
||||
from homeassistant.components.camera.prefs import CameraEntityPreferences
|
||||
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.util.async_ import run_coroutine_threadsafe
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant,
|
||||
@@ -110,7 +109,7 @@ class TestGetImage:
|
||||
"""Grab an image from camera entity."""
|
||||
self.hass.start()
|
||||
|
||||
image = run_coroutine_threadsafe(
|
||||
image = asyncio.run_coroutine_threadsafe(
|
||||
camera.async_get_image(self.hass, "camera.demo_camera"), self.hass.loop
|
||||
).result()
|
||||
|
||||
@@ -123,7 +122,7 @@ class TestGetImage:
|
||||
"homeassistant.helpers.entity_component.EntityComponent." "get_entity",
|
||||
return_value=None,
|
||||
), pytest.raises(HomeAssistantError):
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
camera.async_get_image(self.hass, "camera.demo_camera"), self.hass.loop
|
||||
).result()
|
||||
|
||||
@@ -133,7 +132,7 @@ class TestGetImage:
|
||||
"homeassistant.components.camera.Camera.async_camera_image",
|
||||
side_effect=asyncio.TimeoutError,
|
||||
), pytest.raises(HomeAssistantError):
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
camera.async_get_image(self.hass, "camera.demo_camera"), self.hass.loop
|
||||
).result()
|
||||
|
||||
@@ -143,7 +142,7 @@ class TestGetImage:
|
||||
"homeassistant.components.camera.Camera.async_camera_image",
|
||||
return_value=mock_coro(None),
|
||||
), pytest.raises(HomeAssistantError):
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
camera.async_get_image(self.hass, "camera.demo_camera"), self.hass.loop
|
||||
).result()
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""The tests for the notify.group platform."""
|
||||
import asyncio
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
@@ -6,7 +7,6 @@ from homeassistant.setup import setup_component
|
||||
import homeassistant.components.notify as notify
|
||||
import homeassistant.components.group.notify as group
|
||||
import homeassistant.components.demo.notify as demo
|
||||
from homeassistant.util.async_ import run_coroutine_threadsafe
|
||||
|
||||
from tests.common import assert_setup_component, get_test_home_assistant
|
||||
|
||||
@@ -43,7 +43,7 @@ class TestNotifyGroup(unittest.TestCase):
|
||||
},
|
||||
)
|
||||
|
||||
self.service = run_coroutine_threadsafe(
|
||||
self.service = asyncio.run_coroutine_threadsafe(
|
||||
group.async_get_service(
|
||||
self.hass,
|
||||
{
|
||||
@@ -70,7 +70,7 @@ class TestNotifyGroup(unittest.TestCase):
|
||||
|
||||
def test_send_message_with_data(self):
|
||||
"""Test sending a message with to a notify group."""
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.service.async_send_message(
|
||||
"Hello", title="Test notification", data={"hello": "world"}
|
||||
),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""The tests for Core components."""
|
||||
# pylint: disable=protected-access
|
||||
import asyncio
|
||||
import unittest
|
||||
from unittest.mock import patch, Mock
|
||||
|
||||
@@ -27,7 +28,6 @@ from homeassistant.components.homeassistant import (
|
||||
import homeassistant.helpers.intent as intent
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import entity
|
||||
from homeassistant.util.async_ import run_coroutine_threadsafe
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant,
|
||||
@@ -111,7 +111,7 @@ class TestComponentsCore(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
assert run_coroutine_threadsafe(
|
||||
assert asyncio.run_coroutine_threadsafe(
|
||||
async_setup_component(self.hass, "homeassistant", {}), self.hass.loop
|
||||
).result()
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ from homeassistant.const import (
|
||||
STATE_OFF,
|
||||
STATE_IDLE,
|
||||
)
|
||||
from homeassistant.util.async_ import run_coroutine_threadsafe
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
@@ -162,21 +161,23 @@ class TestAsyncMediaPlayer(unittest.TestCase):
|
||||
def test_volume_up(self):
|
||||
"""Test the volume_up helper function."""
|
||||
assert self.player.volume_level == 0
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_set_volume_level(0.5), self.hass.loop
|
||||
).result()
|
||||
assert self.player.volume_level == 0.5
|
||||
run_coroutine_threadsafe(self.player.async_volume_up(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_volume_up(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.volume_level == 0.6
|
||||
|
||||
def test_volume_down(self):
|
||||
"""Test the volume_down helper function."""
|
||||
assert self.player.volume_level == 0
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_set_volume_level(0.5), self.hass.loop
|
||||
).result()
|
||||
assert self.player.volume_level == 0.5
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_volume_down(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.volume_level == 0.4
|
||||
@@ -184,11 +185,11 @@ class TestAsyncMediaPlayer(unittest.TestCase):
|
||||
def test_media_play_pause(self):
|
||||
"""Test the media_play_pause helper function."""
|
||||
assert self.player.state == STATE_OFF
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_media_play_pause(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.state == STATE_PLAYING
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_media_play_pause(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.state == STATE_PAUSED
|
||||
@@ -196,9 +197,13 @@ class TestAsyncMediaPlayer(unittest.TestCase):
|
||||
def test_toggle(self):
|
||||
"""Test the toggle helper function."""
|
||||
assert self.player.state == STATE_OFF
|
||||
run_coroutine_threadsafe(self.player.async_toggle(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_toggle(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.state == STATE_ON
|
||||
run_coroutine_threadsafe(self.player.async_toggle(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_toggle(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.state == STATE_OFF
|
||||
|
||||
|
||||
@@ -219,7 +224,9 @@ class TestSyncMediaPlayer(unittest.TestCase):
|
||||
assert self.player.volume_level == 0
|
||||
self.player.set_volume_level(0.5)
|
||||
assert self.player.volume_level == 0.5
|
||||
run_coroutine_threadsafe(self.player.async_volume_up(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_volume_up(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.volume_level == 0.7
|
||||
|
||||
def test_volume_down(self):
|
||||
@@ -227,7 +234,7 @@ class TestSyncMediaPlayer(unittest.TestCase):
|
||||
assert self.player.volume_level == 0
|
||||
self.player.set_volume_level(0.5)
|
||||
assert self.player.volume_level == 0.5
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_volume_down(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.volume_level == 0.3
|
||||
@@ -235,11 +242,11 @@ class TestSyncMediaPlayer(unittest.TestCase):
|
||||
def test_media_play_pause(self):
|
||||
"""Test the media_play_pause helper function."""
|
||||
assert self.player.state == STATE_OFF
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_media_play_pause(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.state == STATE_PLAYING
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_media_play_pause(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.state == STATE_PAUSED
|
||||
@@ -247,7 +254,11 @@ class TestSyncMediaPlayer(unittest.TestCase):
|
||||
def test_toggle(self):
|
||||
"""Test the toggle helper function."""
|
||||
assert self.player.state == STATE_OFF
|
||||
run_coroutine_threadsafe(self.player.async_toggle(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_toggle(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.state == STATE_ON
|
||||
run_coroutine_threadsafe(self.player.async_toggle(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.player.async_toggle(), self.hass.loop
|
||||
).result()
|
||||
assert self.player.state == STATE_OFF
|
||||
@@ -5,7 +5,6 @@ import aiohttp
|
||||
|
||||
import homeassistant.components.rest.switch as rest
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.util.async_ import run_coroutine_threadsafe
|
||||
from homeassistant.helpers.template import Template
|
||||
from tests.common import get_test_home_assistant, assert_setup_component
|
||||
|
||||
@@ -23,14 +22,14 @@ class TestRestSwitchSetup:
|
||||
|
||||
def test_setup_missing_config(self):
|
||||
"""Test setup with configuration missing required entries."""
|
||||
assert not run_coroutine_threadsafe(
|
||||
assert not asyncio.run_coroutine_threadsafe(
|
||||
rest.async_setup_platform(self.hass, {"platform": "rest"}, None),
|
||||
self.hass.loop,
|
||||
).result()
|
||||
|
||||
def test_setup_missing_schema(self):
|
||||
"""Test setup with resource missing schema."""
|
||||
assert not run_coroutine_threadsafe(
|
||||
assert not asyncio.run_coroutine_threadsafe(
|
||||
rest.async_setup_platform(
|
||||
self.hass, {"platform": "rest", "resource": "localhost"}, None
|
||||
),
|
||||
@@ -40,7 +39,7 @@ class TestRestSwitchSetup:
|
||||
def test_setup_failed_connect(self, aioclient_mock):
|
||||
"""Test setup when connection error occurs."""
|
||||
aioclient_mock.get("http://localhost", exc=aiohttp.ClientError)
|
||||
assert not run_coroutine_threadsafe(
|
||||
assert not asyncio.run_coroutine_threadsafe(
|
||||
rest.async_setup_platform(
|
||||
self.hass, {"platform": "rest", "resource": "http://localhost"}, None
|
||||
),
|
||||
@@ -50,7 +49,7 @@ class TestRestSwitchSetup:
|
||||
def test_setup_timeout(self, aioclient_mock):
|
||||
"""Test setup when connection timeout occurs."""
|
||||
aioclient_mock.get("http://localhost", exc=asyncio.TimeoutError())
|
||||
assert not run_coroutine_threadsafe(
|
||||
assert not asyncio.run_coroutine_threadsafe(
|
||||
rest.async_setup_platform(
|
||||
self.hass, {"platform": "rest", "resource": "http://localhost"}, None
|
||||
),
|
||||
@@ -131,7 +130,9 @@ class TestRestSwitch:
|
||||
def test_turn_on_success(self, aioclient_mock):
|
||||
"""Test turn_on."""
|
||||
aioclient_mock.post(self.resource, status=200)
|
||||
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.switch.async_turn_on(), self.hass.loop
|
||||
).result()
|
||||
|
||||
assert self.body_on.template == aioclient_mock.mock_calls[-1][2].decode()
|
||||
assert self.switch.is_on
|
||||
@@ -139,7 +140,9 @@ class TestRestSwitch:
|
||||
def test_turn_on_status_not_ok(self, aioclient_mock):
|
||||
"""Test turn_on when error status returned."""
|
||||
aioclient_mock.post(self.resource, status=500)
|
||||
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.switch.async_turn_on(), self.hass.loop
|
||||
).result()
|
||||
|
||||
assert self.body_on.template == aioclient_mock.mock_calls[-1][2].decode()
|
||||
assert self.switch.is_on is None
|
||||
@@ -147,14 +150,18 @@ class TestRestSwitch:
|
||||
def test_turn_on_timeout(self, aioclient_mock):
|
||||
"""Test turn_on when timeout occurs."""
|
||||
aioclient_mock.post(self.resource, status=500)
|
||||
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.switch.async_turn_on(), self.hass.loop
|
||||
).result()
|
||||
|
||||
assert self.switch.is_on is None
|
||||
|
||||
def test_turn_off_success(self, aioclient_mock):
|
||||
"""Test turn_off."""
|
||||
aioclient_mock.post(self.resource, status=200)
|
||||
run_coroutine_threadsafe(self.switch.async_turn_off(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.switch.async_turn_off(), self.hass.loop
|
||||
).result()
|
||||
|
||||
assert self.body_off.template == aioclient_mock.mock_calls[-1][2].decode()
|
||||
assert not self.switch.is_on
|
||||
@@ -162,7 +169,9 @@ class TestRestSwitch:
|
||||
def test_turn_off_status_not_ok(self, aioclient_mock):
|
||||
"""Test turn_off when error status returned."""
|
||||
aioclient_mock.post(self.resource, status=500)
|
||||
run_coroutine_threadsafe(self.switch.async_turn_off(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.switch.async_turn_off(), self.hass.loop
|
||||
).result()
|
||||
|
||||
assert self.body_off.template == aioclient_mock.mock_calls[-1][2].decode()
|
||||
assert self.switch.is_on is None
|
||||
@@ -170,34 +179,44 @@ class TestRestSwitch:
|
||||
def test_turn_off_timeout(self, aioclient_mock):
|
||||
"""Test turn_off when timeout occurs."""
|
||||
aioclient_mock.post(self.resource, exc=asyncio.TimeoutError())
|
||||
run_coroutine_threadsafe(self.switch.async_turn_on(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.switch.async_turn_on(), self.hass.loop
|
||||
).result()
|
||||
|
||||
assert self.switch.is_on is None
|
||||
|
||||
def test_update_when_on(self, aioclient_mock):
|
||||
"""Test update when switch is on."""
|
||||
aioclient_mock.get(self.resource, text=self.body_on.template)
|
||||
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.switch.async_update(), self.hass.loop
|
||||
).result()
|
||||
|
||||
assert self.switch.is_on
|
||||
|
||||
def test_update_when_off(self, aioclient_mock):
|
||||
"""Test update when switch is off."""
|
||||
aioclient_mock.get(self.resource, text=self.body_off.template)
|
||||
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.switch.async_update(), self.hass.loop
|
||||
).result()
|
||||
|
||||
assert not self.switch.is_on
|
||||
|
||||
def test_update_when_unknown(self, aioclient_mock):
|
||||
"""Test update when unknown status returned."""
|
||||
aioclient_mock.get(self.resource, text="unknown status")
|
||||
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.switch.async_update(), self.hass.loop
|
||||
).result()
|
||||
|
||||
assert self.switch.is_on is None
|
||||
|
||||
def test_update_timeout(self, aioclient_mock):
|
||||
"""Test update when timeout occurs."""
|
||||
aioclient_mock.get(self.resource, exc=asyncio.TimeoutError())
|
||||
run_coroutine_threadsafe(self.switch.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
self.switch.async_update(), self.hass.loop
|
||||
).result()
|
||||
|
||||
assert self.switch.is_on is None
|
||||
@@ -1,4 +1,5 @@
|
||||
"""The tests for the Universal Media player platform."""
|
||||
import asyncio
|
||||
from copy import copy
|
||||
import unittest
|
||||
|
||||
@@ -10,7 +11,6 @@ import homeassistant.components.input_number as input_number
|
||||
import homeassistant.components.input_select as input_select
|
||||
import homeassistant.components.media_player as media_player
|
||||
import homeassistant.components.universal.media_player as universal
|
||||
from homeassistant.util.async_ import run_coroutine_threadsafe
|
||||
|
||||
from tests.common import mock_service, get_test_home_assistant
|
||||
|
||||
@@ -298,7 +298,7 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
setup_ok = True
|
||||
try:
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
universal.async_setup_platform(
|
||||
self.hass, validate_config(bad_config), add_entities
|
||||
),
|
||||
@@ -309,7 +309,7 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
assert not setup_ok
|
||||
assert 0 == len(entities)
|
||||
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
universal.async_setup_platform(
|
||||
self.hass, validate_config(config), add_entities
|
||||
),
|
||||
@@ -369,26 +369,26 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
||||
ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"])
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
assert ump._child_state is None
|
||||
|
||||
self.mock_mp_1._state = STATE_PLAYING
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert self.mock_mp_1.entity_id == ump._child_state.entity_id
|
||||
|
||||
self.mock_mp_2._state = STATE_PLAYING
|
||||
self.mock_mp_2.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert self.mock_mp_1.entity_id == ump._child_state.entity_id
|
||||
|
||||
self.mock_mp_1._state = STATE_OFF
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert self.mock_mp_2.entity_id == ump._child_state.entity_id
|
||||
|
||||
def test_name(self):
|
||||
@@ -413,14 +413,14 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
||||
ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"])
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
assert ump.state, STATE_OFF
|
||||
|
||||
self.mock_mp_1._state = STATE_PLAYING
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert STATE_PLAYING == ump.state
|
||||
|
||||
def test_state_with_children_and_attrs(self):
|
||||
@@ -429,22 +429,22 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
||||
ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"])
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
assert STATE_OFF == ump.state
|
||||
|
||||
self.hass.states.set(self.mock_state_switch_id, STATE_ON)
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert STATE_ON == ump.state
|
||||
|
||||
self.mock_mp_1._state = STATE_PLAYING
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert STATE_PLAYING == ump.state
|
||||
|
||||
self.hass.states.set(self.mock_state_switch_id, STATE_OFF)
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert STATE_OFF == ump.state
|
||||
|
||||
def test_volume_level(self):
|
||||
@@ -453,20 +453,20 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
||||
ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"])
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
assert ump.volume_level is None
|
||||
|
||||
self.mock_mp_1._state = STATE_PLAYING
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert 0 == ump.volume_level
|
||||
|
||||
self.mock_mp_1._volume_level = 1
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert 1 == ump.volume_level
|
||||
|
||||
def test_media_image_url(self):
|
||||
@@ -476,7 +476,7 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
||||
ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"])
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
assert ump.media_image_url is None
|
||||
|
||||
@@ -484,7 +484,7 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
self.mock_mp_1._media_image_url = test_url
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
# mock_mp_1 will convert the url to the api proxy url. This test
|
||||
# ensures ump passes through the same url without an additional proxy.
|
||||
assert self.mock_mp_1.entity_picture == ump.entity_picture
|
||||
@@ -495,20 +495,20 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
||||
ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"])
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
assert not ump.is_volume_muted
|
||||
|
||||
self.mock_mp_1._state = STATE_PLAYING
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert not ump.is_volume_muted
|
||||
|
||||
self.mock_mp_1._is_volume_muted = True
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert ump.is_volume_muted
|
||||
|
||||
def test_source_list_children_and_attr(self):
|
||||
@@ -561,7 +561,7 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
||||
ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"])
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
assert 0 == ump.supported_features
|
||||
|
||||
@@ -569,7 +569,7 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
self.mock_mp_1._state = STATE_PLAYING
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
assert 512 == ump.supported_features
|
||||
|
||||
def test_supported_features_children_and_cmds(self):
|
||||
@@ -590,12 +590,12 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
||||
ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"])
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
self.mock_mp_1._state = STATE_PLAYING
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
check_flags = (
|
||||
universal.SUPPORT_TURN_ON
|
||||
@@ -615,16 +615,16 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
||||
ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"])
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
self.mock_mp_1._state = STATE_OFF
|
||||
self.mock_mp_1.schedule_update_ha_state()
|
||||
self.mock_mp_2._state = STATE_OFF
|
||||
self.mock_mp_2.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
run_coroutine_threadsafe(ump.async_turn_off(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_turn_off(), self.hass.loop).result()
|
||||
assert 0 == len(self.mock_mp_1.service_calls["turn_off"])
|
||||
assert 0 == len(self.mock_mp_2.service_calls["turn_off"])
|
||||
|
||||
@@ -634,67 +634,85 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
||||
ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"])
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
self.mock_mp_2._state = STATE_PLAYING
|
||||
self.mock_mp_2.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
run_coroutine_threadsafe(ump.async_turn_off(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_turn_off(), self.hass.loop).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["turn_off"])
|
||||
|
||||
run_coroutine_threadsafe(ump.async_turn_on(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_turn_on(), self.hass.loop).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["turn_on"])
|
||||
|
||||
run_coroutine_threadsafe(ump.async_mute_volume(True), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_mute_volume(True), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["mute_volume"])
|
||||
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_set_volume_level(0.5), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["set_volume_level"])
|
||||
|
||||
run_coroutine_threadsafe(ump.async_media_play(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_media_play(), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["media_play"])
|
||||
|
||||
run_coroutine_threadsafe(ump.async_media_pause(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_media_pause(), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["media_pause"])
|
||||
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_media_previous_track(), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["media_previous_track"])
|
||||
|
||||
run_coroutine_threadsafe(ump.async_media_next_track(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_media_next_track(), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["media_next_track"])
|
||||
|
||||
run_coroutine_threadsafe(ump.async_media_seek(100), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_media_seek(100), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["media_seek"])
|
||||
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_play_media("movie", "batman"), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["play_media"])
|
||||
|
||||
run_coroutine_threadsafe(ump.async_volume_up(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_volume_up(), self.hass.loop).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["volume_up"])
|
||||
|
||||
run_coroutine_threadsafe(ump.async_volume_down(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_volume_down(), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["volume_down"])
|
||||
|
||||
run_coroutine_threadsafe(ump.async_media_play_pause(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_media_play_pause(), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["media_play_pause"])
|
||||
|
||||
run_coroutine_threadsafe(
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_select_source("dvd"), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["select_source"])
|
||||
|
||||
run_coroutine_threadsafe(ump.async_clear_playlist(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_clear_playlist(), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["clear_playlist"])
|
||||
|
||||
run_coroutine_threadsafe(ump.async_set_shuffle(True), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
ump.async_set_shuffle(True), self.hass.loop
|
||||
).result()
|
||||
assert 1 == len(self.mock_mp_2.service_calls["shuffle_set"])
|
||||
|
||||
def test_service_call_to_command(self):
|
||||
@@ -707,12 +725,12 @@ class TestMediaPlayer(unittest.TestCase):
|
||||
|
||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
||||
ump.entity_id = media_player.ENTITY_ID_FORMAT.format(config["name"])
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
self.mock_mp_2._state = STATE_PLAYING
|
||||
self.mock_mp_2.schedule_update_ha_state()
|
||||
self.hass.block_till_done()
|
||||
run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_update(), self.hass.loop).result()
|
||||
|
||||
run_coroutine_threadsafe(ump.async_turn_off(), self.hass.loop).result()
|
||||
asyncio.run_coroutine_threadsafe(ump.async_turn_off(), self.hass.loop).result()
|
||||
assert 1 == len(service)
|
||||
@@ -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
|
||||
Reference in new issue
Block a user