async def test_service_completions(root, expected, hass, services): # pylint: disable=redefined-outer-name """Test service name completion.""" with patch.object(hass.services, "async_services", return_value=services), patch.object( Function, "hass", hass): words = await Function.service_completions(root) assert words == expected
async def test_func_completions(ast_functions, functions, root, expected): # pylint: disable=redefined-outer-name """Test function name completion.""" with patch.object(Function, "ast_functions", ast_functions), patch.object(Function, "functions", functions): words = await Function.func_completions(root) assert words == expected
async def test_service_call(hass): """Test calling a service using the entity_id as a property.""" with patch( "custom_components.pyscript.state.async_get_all_descriptions", return_value={ "test": { "test": {"description": None, "fields": {"entity_id": "blah", "other_service_data": "blah"}} } }, ), patch.object(hass.states, "get", return_value=HassState("test.entity", "True")), patch.object( hass.services, "async_call" ) as call: State.init(hass) await State.get_service_params() func = State.get("test.entity.test") await func(context=Context(id="test"), blocking=True, limit=1, other_service_data="test") assert call.called assert call.call_args[0] == ( "test", "test", {"other_service_data": "test", "entity_id": "test.entity"}, ) assert call.call_args[1] == {"context": Context(id="test"), "blocking": True, "limit": 1} call.reset_mock() func = State.get("test.entity.test") await func(context=Context(id="test"), blocking=False, other_service_data="test") assert call.called assert call.call_args[0] == ( "test", "test", {"other_service_data": "test", "entity_id": "test.entity"}, ) assert call.call_args[1] == {"context": Context(id="test"), "blocking": False}
def test_install_ast_funcs(ast_functions): # pylint: disable=redefined-outer-name """Test installing ast functions.""" ast_ctx = MagicMock() ast_ctx.func.return_value = "ok" with patch.object(Function, "ast_functions", ast_functions): Function.install_ast_funcs(ast_ctx) assert len(ast_ctx.method_calls) == 3
async def test_service_call_params(hass): """Test that hass params get set properly on service calls.""" with patch.object(hass.services, "async_call") as call, patch.object( Function, "service_has_service", return_value=True): Function.init(hass) await Function.service_call("test", "test", context=Context(id="test"), blocking=True, limit=1, other_service_data="test") assert call.called assert call.call_args[0] == ("test", "test", { "other_service_data": "test" }) assert call.call_args[1] == { "context": Context(id="test"), "blocking": True, "limit": 1 } call.reset_mock() await Function.service_call("test", "test", context=Context(id="test"), blocking=False, other_service_data="test") assert call.called assert call.call_args[0] == ("test", "test", { "other_service_data": "test" }) assert call.call_args[1] == { "context": Context(id="test"), "blocking": False } call.reset_mock() await Function.get("test.test")(context=Context(id="test"), blocking=True, limit=1, other_service_data="test") assert call.called assert call.call_args[0] == ("test", "test", { "other_service_data": "test" }) assert call.call_args[1] == { "context": Context(id="test"), "blocking": True, "limit": 1 } call.reset_mock() await Function.get("test.test")(context=Context(id="test"), blocking=False, other_service_data="test") assert call.called assert call.call_args[0] == ("test", "test", { "other_service_data": "test" }) assert call.call_args[1] == { "context": Context(id="test"), "blocking": False } # Stop all tasks to avoid conflicts with other tests await Function.reaper_stop()
async def test_register_aiohuesyncbox_request_error(hass, mock_api): """Test we retry to connect if we cannot connect.""" with patch.object( mock_api, "register", side_effect=aiohuesyncbox.RequestError ), pytest.raises(huesyncbox.CannotConnect): await huesyncbox.async_register_aiohuesyncbox(hass, mock_api)