|
13 | 13 | from custom_components.pyscript.const import CONF_ALLOW_ALL_IMPORTS, CONF_HASS_IS_GLOBAL, DOMAIN, FOLDER |
14 | 14 | from custom_components.pyscript.function import Function |
15 | 15 | from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_STATE_CHANGED |
16 | | -from homeassistant.core import Context |
| 16 | +from homeassistant.core import Context, ServiceRegistry |
17 | 17 | from homeassistant.setup import async_setup_component |
18 | 18 |
|
19 | 19 |
|
@@ -95,10 +95,9 @@ async def test_func_completions( |
95 | 95 | @pytest.mark.asyncio |
96 | 96 | async def test_service_completions(root, expected, hass, services): # pylint: disable=redefined-outer-name |
97 | 97 | """Test service name completion.""" |
98 | | - with patch.object(Function, "hass", hass): |
99 | | - for domain, service_set in services.items(): |
100 | | - for service in service_set: |
101 | | - hass.services.async_register(domain, service, None) |
| 98 | + with patch.object(ServiceRegistry, "async_services", return_value=services), patch.object( |
| 99 | + Function, "hass", hass |
| 100 | + ): |
102 | 101 | words = await Function.service_completions(root) |
103 | 102 | assert words == expected |
104 | 103 |
|
@@ -1248,48 +1247,42 @@ def service_call_exception(): |
1248 | 1247 | @pytest.mark.asyncio |
1249 | 1248 | async def test_service_call_params(hass): |
1250 | 1249 | """Test that hass params get set properly on service calls.""" |
1251 | | - try: |
1252 | | - with patch.object(hass.services, "async_call") as call, patch.object( |
1253 | | - Function, "service_has_service", return_value=True |
1254 | | - ), patch.object( |
1255 | | - hass.services, |
1256 | | - "supports_response", |
1257 | | - return_value="none", |
1258 | | - ): |
1259 | | - Function.init(hass) |
1260 | | - await Function.service_call( |
1261 | | - "test", "test", context=Context(id="test"), blocking=True, other_service_data="test" |
1262 | | - ) |
1263 | | - assert call.called |
1264 | | - assert call.call_args[0] == ("test", "test", {"other_service_data": "test"}) |
1265 | | - assert call.call_args[1] == {"context": Context(id="test"), "blocking": True} |
1266 | | - call.reset_mock() |
1267 | | - |
1268 | | - await Function.service_call( |
1269 | | - "test", "test", context=Context(id="test"), blocking=False, other_service_data="test" |
1270 | | - ) |
1271 | | - assert call.called |
1272 | | - assert call.call_args[0] == ("test", "test", {"other_service_data": "test"}) |
1273 | | - assert call.call_args[1] == {"context": Context(id="test"), "blocking": False} |
1274 | | - call.reset_mock() |
1275 | | - |
1276 | | - await Function.get("test.test")( |
1277 | | - context=Context(id="test"), blocking=True, other_service_data="test" |
1278 | | - ) |
1279 | | - assert call.called |
1280 | | - assert call.call_args[0] == ("test", "test", {"other_service_data": "test"}) |
1281 | | - assert call.call_args[1] == {"context": Context(id="test"), "blocking": True} |
1282 | | - call.reset_mock() |
1283 | | - |
1284 | | - await Function.get("test.test")( |
1285 | | - context=Context(id="test"), blocking=False, other_service_data="test" |
1286 | | - ) |
1287 | | - assert call.called |
1288 | | - assert call.call_args[0] == ("test", "test", {"other_service_data": "test"}) |
1289 | | - assert call.call_args[1] == {"context": Context(id="test"), "blocking": False} |
1290 | | - except AttributeError as e: |
1291 | | - # ignore cleanup exception |
1292 | | - assert str(e) == "'ServiceRegistry' object attribute 'async_call' is read-only" |
| 1250 | + with patch.object(ServiceRegistry, "async_call") as call, patch.object( |
| 1251 | + Function, "service_has_service", return_value=True |
| 1252 | + ), patch.object( |
| 1253 | + ServiceRegistry, |
| 1254 | + "supports_response", |
| 1255 | + return_value="none", |
| 1256 | + ): |
| 1257 | + Function.init(hass) |
| 1258 | + await Function.service_call( |
| 1259 | + "test", "test", context=Context(id="test"), blocking=True, other_service_data="test" |
| 1260 | + ) |
| 1261 | + assert call.called |
| 1262 | + assert call.call_args[0] == ("test", "test", {"other_service_data": "test"}) |
| 1263 | + assert call.call_args[1] == {"context": Context(id="test"), "blocking": True} |
| 1264 | + call.reset_mock() |
| 1265 | + |
| 1266 | + await Function.service_call( |
| 1267 | + "test", "test", context=Context(id="test"), blocking=False, other_service_data="test" |
| 1268 | + ) |
| 1269 | + assert call.called |
| 1270 | + assert call.call_args[0] == ("test", "test", {"other_service_data": "test"}) |
| 1271 | + assert call.call_args[1] == {"context": Context(id="test"), "blocking": False} |
| 1272 | + call.reset_mock() |
| 1273 | + |
| 1274 | + await Function.get("test.test")(context=Context(id="test"), blocking=True, other_service_data="test") |
| 1275 | + assert call.called |
| 1276 | + assert call.call_args[0] == ("test", "test", {"other_service_data": "test"}) |
| 1277 | + assert call.call_args[1] == {"context": Context(id="test"), "blocking": True} |
| 1278 | + call.reset_mock() |
| 1279 | + |
| 1280 | + await Function.get("test.test")( |
| 1281 | + context=Context(id="test"), blocking=False, other_service_data="test" |
| 1282 | + ) |
| 1283 | + assert call.called |
| 1284 | + assert call.call_args[0] == ("test", "test", {"other_service_data": "test"}) |
| 1285 | + assert call.call_args[1] == {"context": Context(id="test"), "blocking": False} |
1293 | 1286 |
|
1294 | 1287 | # Stop all tasks to avoid conflicts with other tests |
1295 | 1288 | await Function.waiter_stop() |
|
0 commit comments